UFCS not working with alias
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 8 06:38:54 PDT 2016
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
More information about the Digitalmars-d-learn
mailing list