Units of Measurement Library: units-d

Simen Kjaeraas via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Fri Apr 1 13:59:20 PDT 2016


On Friday, 1 April 2016 at 19:03:03 UTC, ag0aep6g wrote:
> I dislike that the type depends only on the given name. This 
> effectively means that the names are in a global namespace.
[snip]
> I can't think of a truly nice way to accomplish this, though. 
> As far as I see, it needs a mixin of some kind.

The usual way to fix it would be to include __FILE__ and __LINE__ 
in the template arguments:

// In units/package.d:
struct BaseUnit(string name, string symbol = null, string file = 
__FILE__, size_t line = __LINE__)
{
    // ...
}

// in foo.d:
import experimental.units;
struct A
{
      // Actual type is BaseUnit!("Ampere", "A", "foo", 5):
     alias BaseUnit!("Ampere", "A") Ampere;
     enum ampere = Ampere.init;
}

struct B
{
      // Actual type is BaseUnit!("Ampere", "A", "foo", 12):
     alias BaseUnit!("Ampere", "A") Ampere;
     enum ampere = Ampere.init;
}

void main() {
     assert(!is(B.Ampere == A.Ampere));
}

--
   Simen


More information about the Digitalmars-d-announce mailing list