www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why UFCS doesn't work with "with" statement?

reply Lukanian <nanaipenero forum.com> writes:
Why UFCS doesn't work with "with" statement?

```d
struct MyStruct1
{
   int a, b;
   int sum(){
     return a+b;
   }
}

int difference(ref MyStruct1 self)
{
   return self.a-self.b;

}

// in main()

MyStruct1 s = { 1, 2 };

s.difference().writeln; // works fine

with(s){
   sum().writeln;
   difference().writeln; // error: too few arguments, expected 1, 
got 0
}
```
Jun 09
next sibling parent Doigt <labog outlook.com> writes:
On Monday, 9 June 2025 at 11:39:15 UTC, Lukanian wrote:
 Why UFCS doesn't work with "with" statement?

 ```d
 struct MyStruct1
 {
   int a, b;
   int sum(){
     return a+b;
   }
 }

 int difference(ref MyStruct1 self)
 {
   return self.a-self.b;

 }

 // in main()

 MyStruct1 s = { 1, 2 };

 s.difference().writeln; // works fine

 with(s){
   sum().writeln;
   difference().writeln; // error: too few arguments, expected 
 1, got 0
 }
 ```
It took me a while what you were trying to do, but I think I get it now. I'm not an expert on D, but from what I understand, the reason why it doesn't work is because the difference function is not a method of the struct. The "with" keyword works what's a member of the argument. So essentially yeah, you're calling difference without any argument.
Jun 09
prev sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Monday, 9 June 2025 at 11:39:15 UTC, Lukanian wrote:
 Why UFCS doesn't work with "with" statement?

 ```d
 struct MyStruct1
 {
   int a, b;
   int sum(){
     return a+b;
   }
 }

 int difference(ref MyStruct1 self)
 {
   return self.a-self.b;

 }

 // in main()

 MyStruct1 s = { 1, 2 };

 s.difference().writeln; // works fine

 with(s){
   sum().writeln;
   difference().writeln; // error: too few arguments, expected 
 1, got 0
 }
 ```
The `with` block changes the lookup rule so that unqualified symbols (`sum` and `difference` in this case) are looked up in the scope of the (type of) the `with` argument first, only then normal lookup rules (innermost scope first) apply. What `with(x)` doesn’t do is try to resolve any unqualified symbol `s` as `x.s`. If it did that, your code would work. UFCS doesn’t make members and non-members equivalent, it’s just a calling syntax that works in *some* cases.
Jun 27