www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there an equivavlent to C# boxing in D?

reply IGotD- <nise nise.com> writes:
If you want to store a value type on the heap in D you just use 

wrap the value type into an object. However when you do that 

also have a dynamic type that might solve that but more heavy 
weight).

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing

Is there a possibility to wrap a value type in D around the base 
object class that is otherwise used as the class reference type? 
Would this be a way to use D without raw pointers for heap 
allocated value types?
Feb 11 2022
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 12, 2022 at 12:27:34AM +0000, IGotD- via Digitalmars-d-learn wrote:
 If you want to store a value type on the heap in D you just use "new"

 value type into an object. However when you do that automatic

 dynamic type that might solve that but more heavy weight).
 
 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing
 
 Is there a possibility to wrap a value type in D around the base
 object class that is otherwise used as the class reference type? Would
 this be a way to use D without raw pointers for heap allocated value
 types?
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 -------- T -- Computers aren't intelligent; they only think they are.
Feb 11 2022
next sibling parent reply IGotD- <nise nise.com> writes:
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?
Feb 12 2022
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 12, 2022 at 09:37:56AM +0000, 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(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?
[...] Without alias this it would be harder to pull off, yes. So this particular use case would constitute an example of classes + alias this not being an anti-pattern. (Unless one were to concede that boxed types in general are an anti-pattern... which I'm somewhat inclined to agree with, but I won't press that point. :-P) T -- Having a smoking section in a restaurant is like having a peeing section in a swimming pool. -- Edward Burr
Feb 12 2022
parent Basile B. <b2.temp gmx.com> writes:
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`.
Feb 12 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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 unboxing!
 i = 321; // even this works
 --------
I can't resist adding the convenience function. :) auto boxed(T)(T val) { return new Boxed!T(val); } void main() { // Sweet: auto a = boxed(123); // Sweet too: auto b = 456.boxed; } Ali
Feb 12 2022