ufcs and integer params

David Nadlinger see at klickverbot.at
Wed Jul 18 04:36:36 PDT 2012


On Wednesday, 18 July 2012 at 07:30:10 UTC, Marco Leise wrote:
> Am Sun, 15 Jul 2012 19:17:11 +0200
> schrieb Philippe Sigaud <philippe.sigaud at gmail.com>:
>> […]
>> auto distance = 100.km;
>> auto speed = 120.km/hour;
>> 
>> auto timeToDestination = distance/speed; // timeToDest is a 
>> time (seconds)
>> 
>> The version on Github is grossly limited (it's just a sketch), 
>> but it gives
>> an idea of what's possible. My goal is to code a generic unit 
>> system
>> generator, given user inputs such as a list of units and 
>> sub-units.
>> 
>> […]
>
> Sounds fun. I mean, it makes me happy to see code written like 
> this instead of
> Distance distance = new Kilometers(100);
> Speed speed = Speed.fromDistanceByTime(new Kilometers(120), new 
> Hours(1));

I find multiplication to read much more natural:
---
enum km = kilo * meter;

auto distance = 100.0 * km;
auto speed = 100.0 * km / hour;

auto timeToDest = distance / speed;
---

See http://klickverbot.at/code/units/ for a slightly neglected 
implementation of this scheme. It supports stuff like defining 
new units with arbitrary (potentially runtime) conversion 
factors, properly typechecked affine units (think degrees 
celsius), etc. – but any error messages and symbol names will 
probably look like if the compiler had a seizure.

David


More information about the Digitalmars-d-learn mailing list