Operator overloading through UFCS doesn't work
Tommi
tommitissari at hotmail.com
Sun Oct 14 13:15:14 PDT 2012
On Sunday, 14 October 2012 at 07:14:25 UTC, Maxim Fomin wrote:
> If this request is approved and compiler has opUnary definition
> outside type (which suits better then alias
> this) such function would hijack alias this.
Free functions cannot and must not ever hijack, i.e. modify
existing functionality of a type. Free functions should only be
able to add new functionality to a type. This is what currently
happens with alias this vs free function which is accessed
through UFCS:
struct B
{
void fun()
{
writeln("B.fun()");
}
}
struct A
{
B b;
alias b this;
}
void fun(A a)
{
writeln(".fun(A)");
}
void main()
{
A a;
a.fun(); // prints B.fun() as it should
}
It shouldn't be any different if fun was some operator function,
like opUnary; the free function mustn't hijack type A's existing
functionality (which is currently being provided to A by that
alias this thingy).
More information about the Digitalmars-d-learn
mailing list