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).
-Steve
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?