www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS doubt

reply Antonio <antonio abrevia.net> writes:
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
next sibling parent reply Dennis <dkorpel gmail.com> writes:
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
parent reply Antonio <antonio abrevia.net> writes:
On Thursday, 8 July 2021 at 22:31:49 UTC, Dennis wrote:
 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
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) .
Jul 08 2021
parent Dennis <dkorpel gmail.com> writes:
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
prev sibling next sibling parent jfondren <julian.fondren gmail.com> writes:
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
prev sibling parent Adam Ruppe <destructionator gmail.com> writes:
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