Is D actually |-----------this-----powerful-----------|?

Johannes Pfau nospam at example.com
Sun Jun 10 05:19:39 PDT 2012


Am Sat, 09 Jun 2012 19:07:17 +0200
schrieb "David Piepgrass" <qwertie256 at gmail.com>:

> Oops, forgot the code.
> 
> > "4. Unit inference engine: Given some notation for optionally 
> > indicating units, e.g. unit(value), could a D metaprogram 
> > examine the following code, infer the correct units, and issue 
> > an error message on the last line?
> 
> auto mass = kg(2.0);
> auto accel = 1.0;
> auto force = mass*accel;
> accel += metresPerSecondSquared(9.81); // units of 'force' and 
> 'accel' now known
> force += pounds(3.0);                  // unit mismatch detected

There are some implementations (I think 2 or 3 have been announced on
the newsgroup) which do exactly that. Take a look at this one, for
example:
https://github.com/klickverbot/phobos/blob/units/std/units.d
http://klickverbot.at/code/units/std_units.html
http://klickverbot.at/code/units/std_si.html

There was some talk about including something like that in the standard
library, but that discussion went nowhere.


> P.S. And can D output 'pre-processed' source, i.e. the code after 
> metaprograms have executed?

There's a patch for dmd which does that, but it hasn't
been merged yet.
https://github.com/D-Programming-Language/dmd/pull/426


More information about the Digitalmars-d-learn mailing list