Library Typedefs are fundamentally broken
Meta via Digitalmars-d
digitalmars-d at puremagic.com
Mon Sep 22 06:00:22 PDT 2014
On Monday, 22 September 2014 at 09:39:29 UTC, Don wrote:
> My feeling is that almost every time when you want to create a
> new type from an existing one, you actually want to restrict
> the operations which can be performed on it. (Eg if you have
> typedef money = double; then money*money doesn't make much
> sense). For most typedefs I think you're better off with 'alias
> this'.
`alias this` doesn't restrict what operations can be performed on
the supertype.
struct Money
{
this(double d)
{
amount = d;
}
double amount;
alias amount this;
}
void main()
{
//This doesn't compile without a constructor defined
//that takes a double... I thought alias this took
//care of that, but apparently not
Money m = 2.0;
Money n = m * m;
assert(n == 4.0);
}
More information about the Digitalmars-d
mailing list