ufcs and integer params

Philippe Sigaud philippe.sigaud at gmail.com
Fri Jul 20 05:28:39 PDT 2012


Marco:

>>> auto distance = 100.km;
>>> auto speed = 120.km/hour;

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

Yeah, that was exactly one of my goals :)

David:

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

This is good too. I have code that autogenerate the SI-prefixed versions
(not on Github, I should clean it and push), but thought about offering
prefix functions by themselves. Hmm, looking at it, I guess kilo is just
1000 as an enum in your code? That's a good idea.

Anyway, multiplication is good, I just looked for an excuse to play with
UFCS :)

> 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.

Ah good. I remembered someone posting something on units-checking, but
couldn't find the message. I also have something on celsius/kelvin and
wondered if I needed to go beyond affine, to fully generalize the concept.

Btw, is °C substraction authorized with your module, or multiplication? I
wondered what to forbid and what to authorize.

And I (re)discovered while browsing Wikipedia the numerous temperature
units I saw at school 15 years ago :)

> – but any error messages and symbol names will probably look like if the
compiler had a seizure.

why?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120720/317e72de/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list