Bug 18523 – In the language spec, clarify member function vs UFCS lookup semantics

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dlang.org
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-02-26T04:38:09Z
Last change time
2023-06-21T09:18:47Z
Assigned to
No Owner
Creator
Jonathan Marler

Comments

Comment #0 by johnnymarler — 2018-02-26T04:38:09Z
I was unable to find in the spec what the rule is on function lookup in the case where you have both a member function and a UFCS function with the same name, but the arguments in the call match the UFCS function but not the member function, i.e. struct Foo { void bar(int x) { } } void bar(ref Foo foo, string x) { } void main() { Foo foo; foo.bar("hello"); } Compiling this results in: Error: function Foo.bar (int x) is not callable using argument types (string) It appears that a member function matching will always have priority over a UFCS function even if the arguments don't match the member function. This should be clarified in the spec.
Comment #1 by qs.il.paperinik — 2023-06-21T09:18:47Z
At some point, the spec got refined to: > A free function can be called like a member function when both: > > • The member function does not (or cannot) exist for the object expression > • The free function's first parameter type matches the object expression This explains well that the mere existence of an eponymous member makes UFCS for that name not work.