Bug 15416 – UFCS does not attempt to derefence (but should it?)
Status
RESOLVED
Resolution
WONTFIX
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-12-07T09:28:13Z
Last change time
2022-10-24T13:00:02Z
Keywords
spec
Assigned to
No Owner
Creator
Infiltrator
Comments
Comment #0 by lt.infiltrator — 2015-12-07T09:28:13Z
========================
class C { int x; void bar() { } }
void foo(C) { }
void main() {
auto c = new C;
auto d = &c;
d.bar; // d.bar (C*) does multiple derefs to c.bar (C)
d.foo; // d.foo does not attempt to deref d (C*) to c (C)
}
========================
/d690/f182.d(8): Error: function f182.foo (C _param_0) is not callable using argument types (C*)
========================
This leads to inconsistent behaviour between UFCS functions and true methods. However, should this behaviour be changed for UFCS functions? If it's not extended to non-UFCS functions, it will lead to inconsistent behaviour again (d.foo would work but foo(d) would not).
But if it's extended to all functions, this could get messy for all other functions.
Comment #1 by razvan.nitu1305 — 2022-10-24T13:00:02Z
The pointer is automatically dereferenced only in the presence of the dot (.) operator when a field is queried or a method is invoked. In this situation (`d.foo`) neither of those situations is encountered. Therefore, UFCS is tried and it seems that foo is called with argument d. As you have noticed this is not a situation where D automatically dereferences.
Changing this behavior will lead to massive breakages and it doesn't seem to buy us anything.