Suffix-based literal syntax

Daniel Keep daniel.keep.lists at gmail.com
Wed May 30 19:44:09 PDT 2007



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); }

What if we just got type extensions instead?

Length km(this real r) { return Length(1000, r); }
Length m(this real r) { return Length(1000, r); }

auto distance = 15 .km; // Space to break the float literal
auto otherdis = (10).m; // Or use parens

That way, we don't need any new parsing rules, and this can be extended
to any type, to do pretty much anything you want.

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list