Should AliasThis allow implicit conversion to the subtype AliasThis aliases to?

Aleksandar Ruzicic krckoorascic at gmail.com
Sat May 18 05:03:02 PDT 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;
}


More information about the Digitalmars-d mailing list