Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Alex-Muirhead
Copy link
Collaborator

The util files are currently intertwined with the ntypes files through the Complex datatype, as sometimes we need to call Lua binding functions with either doubles or Complex!doubles. This also causes a duplication of some Lua bindings & functions. With the potential introduction of Dual numbers as an alternative adjoint computation method, this would introduce more duplication.

This merge proposes the use of limited templates, to remove the duplication, and allow for the util files to be decoupled from specific number types. The trait canCastTo is introduced, which allows compile-time checks if a type can be cast to another, either explicitly (using the cast(T) or to!(T) notation) or implicitly (by sub-typing). This is similar to the traits isFloating and isIntegral, however those cannot be extended to user-defined types.

public template canCastTo(T, U) {
    import std.traits : ReturnType;
    // Check if T can be implicitly or explicitly cast to U
    static if ( is(T : U) || is(ReturnType!(T.opCast!U) == U) ) {
        enum canCastTo = true;
    } else {
        enum canCastTo = false;
    }
}

@Alex-Muirhead
Copy link
Collaborator Author

An unintended side-effect of this trait is that more types implement opCast!double than previously expected, including bool, int, and char. This should be taken into consideration as, while they would store the value correctly (as far as D is concerned), it does introduce duplication of effects for the existing lua_pushinteger and lua_pushboolean (although the former uses type size_t).

@Alex-Muirhead Alex-Muirhead self-assigned this Dec 14, 2023
@Alex-Muirhead Alex-Muirhead deleted the separating-utils branch February 22, 2024 07:24
@Alex-Muirhead Alex-Muirhead restored the separating-utils branch February 22, 2024 07:24
@Alex-Muirhead Alex-Muirhead deleted the separating-utils branch February 22, 2024 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant