Is there an equivavlent to C# boxing in D?

IGotD- nise at nise.com
Sat Feb 12 09:37:56 UTC 2022


On Saturday, 12 February 2022 at 00:41:22 UTC, H. S. Teoh wrote:
>
> How about this?
>
> --------
> final class Boxed(T) {
> 	T payload;
> 	alias payload this; // caveat: probably not a good idea in 
> general
> 	this(T val) { payload = val; }
> }
>
> Boxed!int i = new Boxed!int(123);
> int j = i; // hooray, implicit unboxing!
> i = 321; // even this works
> --------
>

Pretty neat solution, you need an extra type but that's not much. 
If alias this would be removed in D, would tricks like these 
suddenly become impossible?




More information about the Digitalmars-d-learn mailing list