On Thursday, 17 May 2018 at 15:25:37 UTC, Sjoerd Nijboer wrote:
Given the following code
`struct Foo(T)
if(isNumeric!T)
{
T t;
.. other code
}
struct Bar(T)
if(isNumeric!T)
{
T t;
.. other code
}
Foo!float foo_float;
Foo!double foo_double;
Bar!float bar_float;
`
I want to make a template mixin that is able to cast one of
these generic structs to the other explicitly. I have a bunch
of these structs and therefore I thought it would make sense to
do it by template mixin. I just don't know what language or
library features I need to use and how to apply them.
`
fooDouble = cast (Foo!double) foo_float; // case [1] if
possible, done by
// an implicit cast.
Else by
//explicit.
fooFloat = cast (Foo!float) foo_double; // case [2]
barFloat = cast (Bar!float) foo_float; // case [3]
`
How would I do this in D?
Before you write anything yourself, check whether
std.conv.castFrom does what you need:
https://dlang.org/phobos/std_conv.html#castFrom