A Huge Bummer When Using alias this
ZombineDev via Digitalmars-d
digitalmars-d at puremagic.com
Thu Mar 24 16:04:32 PDT 2016
On Thursday, 24 March 2016 at 21:26:00 UTC, Jack Stouffer wrote:
> alias this is only useful when not using any function that
> relies on the template constraints in std.traits.
>
> For example
>
> import std.traits;
>
> void someFunc(N)(N val) if (isNumeric!N)
> {
> int b = val;
> }
>
> void main()
> {
> import std.typecons;
> Nullable!int a = 42;
>
> someFunc(a);
> }
>
> $ dmd test.d
> test.d(13): Error: template someFunc cannot deduce function
> from argument types !()(Nullable!(int))
>
> Removing the template constraint makes it compile and run just
> fine. What's a good work around for these types of issues?
`isNumeric(T)` explicitly allows only built-in types (int, float,
etc.) and prevents user-defined types.
Use `if (is(NumericTypeOf!T N))` if you want to allow types `T`
that can convert to some numeric type `N`.
More information about the Digitalmars-d
mailing list