www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot use UFCS in lambda?

reply berni <someone somemail.de> writes:
The following program does not compile:

import std.stdio;
import std.algorithm;

struct A
{
    struct S
    {
        int x;
    }

    const bool foo(in S s, in int k)
    {
        return s.x<k;
    }

    void bar()
    {
        [S(1),S(2),S(3),S(4),S(5)].filter!(a=>a.foo(3)).writeln;
    }
}

void main()
{
    A().bar();
}
I get (using rdmd):
test.d(18): Error: no property foo for type S
/usr/include/dmd/phobos/std/algorithm/iteration.d(1108):        
instantiated from here: >FilterResult!(__lambda1, S[])
test.d(18):        instantiated from here: filter!(S[])
When I replace the UFCS-Syntax in the lambda by a function call it works:
 [S(1),S(2),S(3),S(4),S(5)].filter!(a=>foo(a,3)).writeln;
Where is my mistake?
Sep 16 2018
parent reply Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Sunday, 16 September 2018 at 09:46:15 UTC, berni wrote:
 Where is my mistake?
Lambdas are not the issue here. The problem is more general: you can only use top-level symbols in UFCS. You can use an identity alias template to bypass this: https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/ (search for UFCS in the page).
Sep 16 2018
parent reply berni <someone somemail.de> writes:
 The problem is more general: you can only use top-level symbols 
 in UFCS.

 You can use an identity alias template to bypass this:
 https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/
 (search for UFCS in the page).
Good to know. :-)
Sep 16 2018
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 16 September 2018 at 10:55:43 UTC, berni wrote:
 The problem is more general: you can only use top-level 
 symbols in UFCS.

 You can use an identity alias template to bypass this:
 https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/
 (search for UFCS in the page).
Good to know. :-)
Worth noting, this is now included in Phobos as `std.meta.Alias`.
Sep 16 2018