Making uniform function call syntax more complete a feature
Thiez
thiezz at gmail.com
Thu Jul 12 06:18:59 PDT 2012
On Thursday, 12 July 2012 at 12:43:24 UTC, Tommi wrote:
> On Thursday, 12 July 2012 at 10:05:16 UTC, Thiez wrote:
>> Have you considered adding operator overloading using UFCS
>> while you're at it?
>
> I assumed it's already possible to add operators
> non-intrusively, because operators are just syntactic sugar for
> method calls:
>
> ++var; // actual code
> var.opUnary!"++"(); // lowered once
> opUnary!"++"(var); // lowered twice (if necessary)
>
> If you're talking about overloading existing operators (which
> have been implemented as member functions) non-intrusively for
> other types, then I don't know, doesn't it work?
I actually tried those yesterday (with opEquals and opCmp on
structs) and couldn't get it to work. Code still used what
appeared to be an automatically generated opEquals (that appears
to perform a bitwise comparison) instead of my UFCS opEquals.
It's already quite obvious that the compiler does not obey its
own rewrite rules (see
http://dlang.org/operatoroverloading.html#compare) Consider opCmp:
a < b
is rewritten to
a.opCmp(b) < 0
or
b.opCmp(a) > 0
Let's assume the first rule is always chosen. According to the
very rewrite rule we just applied, this must be rewritten to
a.opCmp(b).opCmp(0) < 0
which must be rewritten to
a.opCmp(b).opCmp(0).opCmp(0) < 0
and then
a.opCmp(b).opCmp(0).opCmp(0).opCmp(0) < 0
and so on, to infinity.
It seems quite obvious the compiler does not rewrite compares on
integers or all hell would break loose... The language reference
should be more specific about these things.
More information about the Digitalmars-d
mailing list