A Huge Bummer When Using alias this

Jerry via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 24 15:31:01 PDT 2016


On Thursday, 24 March 2016 at 21:26:00 UTC, Jack Stouffer wrote:
> void someFunc(N)(N val) if (isNumeric!N)
> {
>     int b = val;
> }
>
> void main()
> {
>     import std.typecons;
>     Nullable!int a = 42;
>
>     someFunc(a);
> }

Basicly you need to test if param is a Nullable and if it is get 
the
template param first param.
The FirstTemplateParamMatches template is more psuedo code.
Maybe there is a nicer way to get the first template param.

void someFunc(N)(N val)
    if(isInstanceOf!(Nullable, N)
         && FirstTemplateParamMatches!(isNumeric, N)|| 
isNumeric!(N))
{}

template TemplateParamMatches(alias C, T)
{
      enum TemplateParamMatches =
          
C(mixin("fullyQualifiedName!(T).split(\"!(\").split(\")\")"));
}


But if you want to use several functions you can do some template 
overloading.
Then one for Nullable which simply forwards the function.

void someFunc(N)(Nullable!N val)
     if(isNumeric!(N))
{
     someFunc(val.get);
}


More information about the Digitalmars-d mailing list