On 4/6/15 2:00 PM, Szymon Gatner wrote:
On Monday, 6 April 2015 at 17:53:13 UTC, Steven Schveighoffer wrote:
On 4/6/15 12:23 PM, Szymon Gatner wrote:
Hi,
I am surprised that this doesn't work:
class Foo
{
void bar(string) {}
}
void bar(Foo foo, int i)
{
}
auto foo = new Foo();
foo.bar(123); // <=== error
causing compilation error:
main.d(24): Error: function main.Foo.bar (string _param_0) is not
callable using argument types (int)
does UFCS now work with method overloading? I know it is not a syntax
error because changing the name of int version of bar to bar2 and
calling foo.bar2(123) works fine.
You can't do this. UFCS cannot add overloads, it can only add whole
overload sets (if not already present).
Why is that? The use case is to provide a set of convenience "extension
methods" to a basic interface. Say, given:
interface Subject
{
void add(SomeInterface obj);
}
// and then
void add(Subject a, Type1 v1)
{
a.add(convertToSomeInterface(v1));
}
void add(Subject a, Type2 v2)
{
a.add(convertToSomeInterface(v2));
}
this way client can just implement Subject interface and still use it
with types Type1 and Type2. C# allows that, why D does not?
In D, the symbol itself is used to find the appropriate "overload set"
and then the parameters are used within the set to figure out the
specific overload to use. Overload sets have different precedence, with
I think members having the highest precedent, and likely UFCS having the
lowest. But once an overload set is found, anything defined outside that
set is not seen.
This is done in part to prevent hijacking of functions. For example, if
you wanted to change the meaning of some code by defining a better
overload match.
This doesn't mean it couldn't be changed, but it is definitely
implemented as designed.
-Steve