Library Typedefs are fundamentally broken

Meta via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 23 11:02:56 PDT 2014


Can someone clarify how exactly Typedef should work? I'd love to 
make a pull request to fix up whatever needs fixing, but it seems 
like there are a couple different notions of how it should work, 
and it's quite confusing. This seems very similar to the argument 
over enums (which turned out to not be as implicitly convertible 
to their base types as I thought they were).

double doSomething(double d) { return d + 1.0; }

void main()
{
     typedef double money = 0.0;

     money m = 4.0;
     money n = doSomething(m);
     assert(n == 5.0);

     //Allowed?
     money o = m * n;
     assert(o == 20.0);

     //Allowed?
     money p = m * 2.0;
     assert(p == 8.0);

     //Etc. for the other arithmetic operations

     //Passes or fails?
     assert(!isNaN(p.init));
     //Do typedef'd types inherit the properties
     //of their base classes?
     assert(p.min_normal == double.min_normal);


     typedef float dollars = 0.0f;
     typedef float cents = 0.0f;

     dollars d = 1.0f;
     //Allowed?
     cents c = d;
     cents c = cast(cents)d;
     cents c = cast(float)d;

     //Allowed?
     float f = d
     float f = d - c;
     float f = d - 1.0f;

     //Are these allowed? If so, what
     //are the resultant types?
     auto g = c * d;
     auto h = d * 1.0f;

     //Etc., etc.


     typedef int index;

     int[] arr = [0, 1, 2];
     index a = 1;
     //Allowed?
     assert(arr[a] == 1);
     index b = 4;
     arr ~= b;
}



More information about the Digitalmars-d mailing list