digitalmars.D.learn - UFCS doubt
- Antonio (28/28) Jul 08 2021 In this example (extracted from
- Dennis (3/10) Jul 08 2021 https://dlang.org/spec/function.html#pseudo-member
- Antonio (27/39) Jul 08 2021 Thanks.
- Dennis (5/7) Jul 08 2021 Yeah, 50/285 people answering the question "What language
- jfondren (4/7) Jul 08 2021 https://dlang.org/spec/function.html#pseudo-member
- Adam Ruppe (4/6) Jul 08 2021 UFCS only works with functions defined at top level, not nested
In this example (extracted from https://digitalmars.com/articles/b68.html), this works: ``` class C { int a; int foo(int i) { return i + a; } } auto mfp = (C self, int i)=>self.foo(i); void main(){ auto c = new C; assert( c.mfp(20)==20); } ``` but this fails ``` class C { int a; int foo(int i) { return i + a; } } void main(){ auto mfp = (C self, int i)=>self.foo(i); auto c = new C; assert( c.mfp(20)==20); } ``` onlineapp.d(9): Error: no property `mfp` for type `onlineapp.C` I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why?
Jul 08 2021
On Thursday, 8 July 2021 at 22:24:26 UTC, Antonio wrote:I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why?UFCS does not work for nested functions.Functions declared in a local scope are not found when searching for a matching UFCS function. ... Rationale: Local function symbols are not considered by UFCS to avoid unexpected name conflicts. See below problematic examples.https://dlang.org/spec/function.html#pseudo-member
Jul 08 2021
On Thursday, 8 July 2021 at 22:31:49 UTC, Dennis wrote:On Thursday, 8 July 2021 at 22:24:26 UTC, Antonio wrote:Thanks. I read the example and the assumption of "name conflict" does not seem to be justified (from my point of view) i.e. Without dot notation, this example must fail ``` int front(int[] arr) { return arr[0]; } void main() { int[] a =[1,2,3]; auto front = 1; // front is now a variable auto y = front(a); // Error, front is not a function } ``` Changing to y = a.front() should not change the behavior (it is only a notation change )... but it does!!! ``` int front(int[] arr) { return arr[0]; } void main() { int[] a =[1,2,3]; auto front = 1; // front is now a variable auto y = a.front() // NO ERROR!!! } ``` "It works as described in the manual, not as expected" (from MySQL haters club :-p) .I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why?UFCS does not work for nested functions.Functions declared in a local scope are not found when searching for a matching UFCS function. ... Rationale: Local function symbols are not considered by UFCS to avoid unexpected name conflicts. See below problematic examples.https://dlang.org/spec/function.html#pseudo-member
Jul 08 2021
On Thursday, 8 July 2021 at 23:31:57 UTC, Antonio wrote:"It works as described in the manual, not as expected" (from MySQL haters club :-p) .Yeah, 50/285 people answering the question "What language features do you miss?" chose "UFCS for local symbols" in the [State of D survey (2018)](https://rawgit.com/wilzbach/state-of-d/master/report.html), but no one has championed a language change for it yet.
Jul 08 2021
On Thursday, 8 July 2021 at 22:24:26 UTC, Antonio wrote:onlineapp.d(9): Error: no property `mfp` for type `onlineapp.C` I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why?https://dlang.org/spec/function.html#pseudo-member 6. Functions declared in a local scope are not found when searching for a matching UFCS function.
Jul 08 2021
On Thursday, 8 July 2021 at 22:24:26 UTC, Antonio wrote:I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why?UFCS only works with functions defined at top level, not nested inside other functions. That's just how it is defined i don't really know why.
Jul 08 2021