Suffix-based literal syntax

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed May 30 15:59:43 PDT 2007


Actually in the case you present, there is no ambiguity.  There /is/ overloading on 
return-type, just not overloading on /only/ return-type.  (I had the same impression 
initially, but not so long ago was corrected on it -- to great joy, as it happens.)

Ambiguities caused by implicit casts can be caught by enabling warnings, and for that 
matter I'd expect the compiler to say something about the double-casting going on here, 
since neither of the return types are 'long'.  (Most likely, in this case, the 'uint(int)' 
version would be selected, and since a 'uint' can fit safely in a 'long', there should be 
no mis-behavior.)

For that matter, your example is best defined as a template, seeing as the implementation 
is identical.  Probably quite a lot of these suffixes would be templates.

All in all, I'm neutral on the idea.  It might have its uses, and would certainly find 
followers amongst the mathematicians among us... but I'd bet most people would rarely if 
ever have a significant use for it.

-- Chris Nicholson-Sauls

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
> 
> Reiner Pope Wrote:
> 
>> Reiner Pope wrote:
>>> I envisage a usage something like this:
>>>
>>> struct LengthKM { ... }
>>> alias LengthKM.opCall opSuff_km;
>>> // Of course this would actually be auto generated by a template, but 
>>> still...
>> Oh, and you might want:
>>
>> struct Length {...}
>>
>> Length opSuff_km(real r) { return Length(1000, r); }
>> Length opSuff_m(real r) { return Length(1, r); }
>> Length opSuff_mm(real r) { return Length(0.001, r); }
> 



More information about the Digitalmars-d mailing list