Neat: UFCS for integer dot operator suffix

bearophile bearophileHUGS at lycos.com
Mon Sep 24 05:28:11 PDT 2012


monarch_dodra:

> I tried this, and found it neat that it works:

Lately I was thinking about this topic (because of a program that 
mixes many doubles that are instead different types).

// F#
let gravityOnEarth = 9.81<m/s^2>
let heightOfMyOfficeWindow = 3.5<m>
let speedOfImpact = sqrt(2.0 * gravityOnEarth * 
heightOfMyOfficeWindow)


So is this a good syntax for an hypothetical Phobos library?

Acceleration gravityOnEarth = Units!"m/s^2"(9.81);
alias Units U;
auto heightOfMyOfficeWindow = U!`m`(3.5);
alias U!"g" grams; // a @property
auto sugarAmount1 = Grams(10.5);
auto sugarAmount2 = 10.5.Grams;
Length dist1 = 3.U!"cm";
auto dist2 = 2.U!q{cm^1};
alias U!"cm^2" cm2;
assert(dist1 * dist2 == 6.cm2);
mixin NewUnit!"degrees";
auto myAngularSpeed = 90.U!"degrees/s";

Bye,
bearophile


More information about the Digitalmars-d mailing list