Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 16, 2018 Cannot use UFCS in lambda? | ||||
---|---|---|---|---|
| ||||
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? |
September 16, 2018 Re: Cannot use UFCS in lambda? | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | 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). |
September 16, 2018 Re: Cannot use UFCS in lambda? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | > 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. :-)
|
September 16, 2018 Re: Cannot use UFCS in lambda? | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | 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`.
|
Copyright © 1999-2021 by the D Language Foundation