Suffix-based literal syntax

Reiner Pope some at address.com
Wed May 30 18:01:22 PDT 2007


Robert Fraser wrote:
> Just had a thought: That's all well and good, but what types do these take? A number could be floating-point or not (there are multiple floating point types...), etc., etc. Operator overloading might work for some things, but since there's no return-type operator overloading, there could be ambiguity. Consider:
> 
> uint opSuff_abs(int value) { return abs >= 0 ? abs : -abs; }
> ulong ofSuff_abs(long value) { return abs >= 0 ? abs : -abs; }
> 
> // ...
> 
> uint foo = -5abs; // While we know it has to call the int method; there's no return-type overloading
> long bar = -5abs; // Even with return-type overloading, implicit casts still add ambiguity
I'm not exactly sure on the overloading rules, but that doesn't matter. 
The way I would approach that is:

uint opSuff_abs(int value) { ... }
ulong opSuff_absL(long value) { ... }
alias opSuff_absL opSuff_abs;

auto foo = -5abs; // rewritten as abs(-5) -- compiler chooses the int 
overload
auto bar = -100000000000abs; // rewritten as abs(-1000000000000) -- 
compiler chooses long overload since that literal is long
auto bam = -5absL; // only long version is available.

So, if there is a need to specify overloads, it's up to the library 
implementor to provide specially-named suffixes.

    -- Reiner



More information about the Digitalmars-d mailing list