Suffix-based literal syntax

Robert Fraser fraserofthenight at gmail.com
Wed May 30 15:46:40 PDT 2007


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