Multiple implicit type converters

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 11 13:00:00 PDT 2015


On Friday, 11 September 2015 at 19:51:09 UTC, Dave Akers wrote:
> Would it be possible to create it as an 'as' template?

Yeah, the way I'd do it is something like:

T as(T)() {
    import std.traits;
    static if(isIntegral!T)
        return to!T(convert_to_some_int);
    else static if(isSomeString!T)
        return to!T(convert_to_some_string);
    else /// and so on and so forth
}


These isX templates from std.traits will help you break down a 
gazillion potential types into just a handful of type families 
which are easier to handle.

But you could also just static if(is(T == something)) or whatever 
to list the types you do want to support.


More information about the Digitalmars-d-learn mailing list