Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 12, 2015 alias butAtLeast = max; 5.butAtLeast(6); | ||||
---|---|---|---|---|
| ||||
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 |
December 12, 2015 Re: alias butAtLeast = max; 5.butAtLeast(6); | ||||
---|---|---|---|---|
| ||||
Posted in reply to SimonN | 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.
|
December 12, 2015 Re: alias butAtLeast = max; 5.butAtLeast(6); | ||||
---|---|---|---|---|
| ||||
Posted in reply to SimonN | 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/ |
December 12, 2015 Re: alias butAtLeast = max; 5.butAtLeast(6); | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZombineDev | > 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 |
Copyright © 1999-2021 by the D Language Foundation