digitalmars.D - Should AliasThis allow implicit conversion to the subtype AliasThis
- Dylan Knutson (21/21) May 18 2013 I found out about AliasThis on the IRC, and it's pretty neat.
- Dylan Knutson (3/5) May 18 2013 should be
- Dylan Knutson (11/11) May 18 2013 oh my; one more typo (I really wish these forums had edit
- Aleksandar Ruzicic (6/27) May 18 2013 Just overload assignment operator for that.
- Aleksandar Ruzicic (1/5) May 18 2013 Oops, that should be IntPair opAssign(int[2] rhs)
I found out about AliasThis on the IRC, and it's pretty neat. I've got a suggestion for it however: Assigning a value to a class/struct which has aliased a member to this would forward the assignment to that member. For instance: ---- struct IntPair { private int _pair; int left() { return _pair[0]; } int right() { return _pair[1]; } } IntPair a = [1, 2]; // _pair = [1, 2] IntPair b; b.left(); //0 b = [3, 4]; b.left(); //3 ---- Thoughts? I think it'd be very useful for defining types that look and act like built in primitives, but can have arbitrary methods and operator overloads attached to them. Thanks to <jA_cOp> for bringing up AliasThis in IRC :-)
May 18 2013
Oh this is embarrassing; On Saturday, 18 May 2013 at 07:43:57 UTC, Dylan Knutson wrote:private int _pair;should beprivate int[2] _pair;
May 18 2013
oh my; one more typo (I really wish these forums had edit functionality) struct definition: ---- struct IntPair { private int[] _pair; int left() { return _pair[0]; } int right() { return _pair[1]; } alias _pair this; } ----
May 18 2013
On Saturday, 18 May 2013 at 07:43:57 UTC, Dylan Knutson wrote:I found out about AliasThis on the IRC, and it's pretty neat. I've got a suggestion for it however: Assigning a value to a class/struct which has aliased a member to this would forward the assignment to that member. For instance: ---- struct IntPair { private int _pair; int left() { return _pair[0]; } int right() { return _pair[1]; } } IntPair a = [1, 2]; // _pair = [1, 2] IntPair b; b.left(); //0 b = [3, 4]; b.left(); //3 ---- Thoughts? I think it'd be very useful for defining types that look and act like built in primitives, but can have arbitrary methods and operator overloads attached to them. Thanks to <jA_cOp> for bringing up AliasThis in IRC :-)Just overload assignment operator for that. int[2] opBinary(string op)(int[2] rhs) if (op == "=") { _pair = rhs; return rhs; }
May 18 2013
int[2] opBinary(string op)(int[2] rhs) if (op == "=") { _pair = rhs; return rhs; }Oops, that should be IntPair opAssign(int[2] rhs)
May 18 2013