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);
}