alias butAtLeast = max; 5.butAtLeast(6);

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 12 05:34:09 PST 2015


On Saturday, 12 December 2015 at 12:43:36 UTC, SimonN wrote:
> DMD v2.069.2-b1 on Linux.
>
>     import std.algorithm;
>
>     int a = max(5, 6);            // works, a == 6
>     int b = max!(int, int)(5, 6); // works, manual instantiation
>     int c = 5.max(6);             // works, UFCS call
>
> I would like to use the last syntax, but with an alias.
>
>     alias butAtLeast = max;   // works
>     int d = butAtLeast(5, 6); // works
>     int e = 5.butAtLeast(6);  // error: no property 
> 'butAtLeast' for type 'int'
>
> Aliasing the instantiated function 'max!(int, int)' instead of 
> aliasing 'max' doesn't help: The 'int e' line will fail with 
> the exact same error.
>
> Can I get the alias to work somehow in an UFCS chain?
>
> -- Simon

This is due to limitation of function-local aliases. If you put 
the alias outside it will work: 
http://dpaste.dzfl.pl/4fb06cbbfad2.

Perhaps a simpler way achieve this is to use renamed imports: 
http://dpaste.dzfl.pl/08774a538fe9

The Identity template can also helpful in some situations: 
http://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/


More information about the Digitalmars-d-learn mailing list