Re: Is there an equivavlent to C# boxing in D?

2022-02-12 Thread Ali Çehreli via Digitalmars-d-learn
On 2/11/22 16:41, 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 unboxi

Re: Is there an equivavlent to C# boxing in D?

2022-02-12 Thread Basile B. via Digitalmars-d-learn
On Saturday, 12 February 2022 at 16:50:16 UTC, H. S. Teoh wrote: Without alias this it would be harder to pull off, yes. I don't see any other way that allows to unbox *implictly*. That would require a new operator. Something like opCast but more permissive, that works without `cast`.

Re: Is there an equivavlent to C# boxing in D?

2022-02-12 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Feb 12, 2022 at 09:37:56AM +, IGotD- via Digitalmars-d-learn wrote: > On Saturday, 12 February 2022 at 00:41:22 UTC, H. S. Teoh wrote: [...] > > > > final class Boxed(T) { > > T payload; > > alias payload this; // caveat: probably not a good idea in general > > this

Re: Is there an equivavlent to C# boxing in D?

2022-02-12 Thread IGotD- via Digitalmars-d-learn
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; // hoo

Re: Is there an equivavlent to C# boxing in D?

2022-02-11 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Feb 12, 2022 at 12:27:34AM +, IGotD- via Digitalmars-d-learn wrote: > If you want to store a value type on the heap in D you just use "new" > and a pointer to the type. The same thing in C# would be to wrap the > value type into an object. However when you do that automatic > conversion

Is there an equivavlent to C# boxing in D?

2022-02-11 Thread IGotD- via Digitalmars-d-learn
If you want to store a value type on the heap in D you just use "new" and a pointer to the type. The same thing in C# would be to wrap the value type into an object. However when you do that automatic conversion without a cast seems not to be possible (C# also have a dynamic type that might sol