www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS not working with alias

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I just noticed ufcs does not work with alias. Is this limitation 
needed?

void foo(int a) {}

void main()
{
	alias bar = foo;
	3.foo();
	3.bar();
}

Last line fails  with "no property 'bar' for type int.

Should I open an enhancement request?

Kind regards
André
Sep 07 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/09/2016 8:06 PM, Andre Pany wrote:
 Should I open an enhancement request?
No. It works outside of the function (part of lookup rules).
Sep 07 2016
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Wednesday, 7 September 2016 at 08:08:34 UTC, rikki cattermole 
wrote:
 On 07/09/2016 8:06 PM, Andre Pany wrote:
 Should I open an enhancement request?
No. It works outside of the function (part of lookup rules).
I simplified my example too much. Yes in the example above I can move the alias outside the main function. Here is a more complex example. As I use the input parameter args in the alias, I cannot move the alias outside the main function. import std.algorithm; bool fulfillsKeyPredicate(string s, string t) {return true;} void main(string[] args) { alias keyPredicateFilter = filter!(e => e.fulfillsKeyPredicate(args[0])); string[] arr; keyPredicateFilter(arr); arr.keyPredicateFilter; } Is there a workaround? Or is still a valid scenario to change the lookup rules? Kind regards André
Sep 07 2016
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/09/2016 8:26 PM, Andre Pany wrote:
 On Wednesday, 7 September 2016 at 08:08:34 UTC, rikki cattermole wrote:
 On 07/09/2016 8:06 PM, Andre Pany wrote:
 Should I open an enhancement request?
No. It works outside of the function (part of lookup rules).
I simplified my example too much. Yes in the example above I can move the alias outside the main function. Here is a more complex example. As I use the input parameter args in the alias, I cannot move the alias outside the main function. import std.algorithm; bool fulfillsKeyPredicate(string s, string t) {return true;} void main(string[] args) { alias keyPredicateFilter = filter!(e => e.fulfillsKeyPredicate(args[0])); string[] arr; keyPredicateFilter(arr); arr.keyPredicateFilter; } Is there a workaround? Or is still a valid scenario to change the lookup rules?
People have tried, this is the behavior as designed. The workaround is simple, don't use UFCS. I won't repeat the explanation or reasoning here, plenty of posts on it ;)
Sep 07 2016
parent Andre Pany <andre s-e-a-p.de> writes:
On Wednesday, 7 September 2016 at 08:35:26 UTC, rikki cattermole 
wrote:
 On 07/09/2016 8:26 PM, Andre Pany wrote:
 [...]
People have tried, this is the behavior as designed. The workaround is simple, don't use UFCS. I won't repeat the explanation or reasoning here, plenty of posts on it ;)
Thanks, this answer my question. Kind regards André
Sep 07 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/7/16 4:26 AM, Andre Pany wrote:
 On Wednesday, 7 September 2016 at 08:08:34 UTC, rikki cattermole wrote:
 On 07/09/2016 8:06 PM, Andre Pany wrote:
 Should I open an enhancement request?
No. It works outside of the function (part of lookup rules).
I simplified my example too much. Yes in the example above I can move the alias outside the main function. Here is a more complex example. As I use the input parameter args in the alias, I cannot move the alias outside the main function. import std.algorithm; bool fulfillsKeyPredicate(string s, string t) {return true;} void main(string[] args) { alias keyPredicateFilter = filter!(e => e.fulfillsKeyPredicate(args[0])); string[] arr; keyPredicateFilter(arr); arr.keyPredicateFilter; } Is there a workaround? Or is still a valid scenario to change the lookup rules?
There is a workaround, identified by Vladimir Panteleev (https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/): import std.algorithm; bool fulfillsKeyPredicate(string s, string t) {return true;} alias I(alias x) = x; void main(string[] args) { alias keyPredicateFilter = filter!(e => e.fulfillsKeyPredicate(args[0])); string[] arr; keyPredicateFilter(arr); arr.I!keyPredicateFilter; } Unfortunately, you can't alias the I!someSymbol, as that has the same problem. -Steve
Sep 08 2016
parent Andre Pany <andre s-e-a-p.de> writes:
On Thursday, 8 September 2016 at 13:38:54 UTC, Steven 
Schveighoffer wrote:
 There is a workaround, identified by Vladimir Panteleev 
 (https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/):


 import std.algorithm;
 bool fulfillsKeyPredicate(string s, string t) {return true;}

 alias I(alias x) = x;

 void main(string[] args)
 {
     alias keyPredicateFilter = filter!(e => 
 e.fulfillsKeyPredicate(args[0]));
     string[] arr;

     keyPredicateFilter(arr);
     arr.I!keyPredicateFilter;
 }

 Unfortunately, you can't alias the I!someSymbol, as that has 
 the same problem.

 -Steve
Thanks for this insight. In my case I prepare a presentation about D (audience doesn't now anything about D) and tried to make my D code as beautiful as possible. As solution I created now a free template method and hide it in a library module. T[] filterByKeyPredicate(T)(T[] arr, string[string] keyPredicate) { ...filter!.... } This way I can use UFCS. Kind regards André
Sep 08 2016