Making uniform function call syntax more complete a feature
Tommi
tommitissari at hotmail.com
Thu Jul 12 06:51:41 PDT 2012
On Thursday, 12 July 2012 at 13:19:00 UTC, Thiez wrote:
> 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.
Quote from 'The D Programming Language' chapter 12.1:
"D’s approach to operator overloading is simple: whenever at
least one participant in an operator expression is of
user-defined type, the compiler rewrites the expression into a
regular method call with a specific name. Then the regular
language rules apply."
So, assuming opCmp returns integer, then a.opCmp(b) < 0 doesn't
get rewritten (lowered) any further, because user-defined types
are not involved.
More information about the Digitalmars-d
mailing list