www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias butAtLeast = max; 5.butAtLeast(6);

reply SimonN <eiderdaus gmail.com> writes:
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
Dec 12 2015
next sibling parent JR <sunspyre gmail.com> writes:
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
By putting it in the top level. I believe this is intentional but I don't remember the reasoning.
Dec 12 2015
prev sibling parent reply ZombineDev <valid_email he.re> writes:
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/
Dec 12 2015
parent SimonN <eiderdaus gmail.com> writes:
 By putting it in the top level. I believe this is intentional 
 but I don't remember the reasoning.
On Saturday, 12 December 2015 at 13:34:09 UTC, ZombineDev wrote:
 This is due to limitation of function-local aliases. If you put 
 the alias outside it will work: 
 http://dpaste.dzfl.pl/4fb06cbbfad2.
Thanks for both of these quick replies -- doing it similar to that example, it works for me too now. Good to know this little workaround. I'd love to see the design reason for the limitation. Right now, the error message (property doesn't exist at all) seems to be misleading. But getting all corner cases right is always tricky. :-)
 Perhaps a simpler way achieve this is to use renamed imports:
 The Identity template can also helpful in some situations:
Thanks, will read for inspiration! -- Simon
Dec 12 2015