www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a generic type such as void* or C#'s object?

reply Jack <jckj33 gmail.com> writes:



class A {
   object foo;
}

and

var a = new A();
a.foo = any class...;

does D have something like this or template parameters are used 
instead of?
Feb 05 2021
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Saturday, 6 February 2021 at 02:01:28 UTC, Jack wrote:



 class A {
   object foo;
 }

 and

 var a = new A();
 a.foo = any class...;

 does D have something like this or template parameters are used 
 instead of?
If it's just for classes, you can use `Object` [1], which is the universal base class. For absolutely any type at all, you can try using `Variant` [2], though it has a few limitations. [1] http://druntime.dpldocs.info/object.Object.html [2] http://phobos.dpldocs.info/std.variant.Variant.html
Feb 05 2021
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 06, 2021 at 02:01:28AM +0000, Jack via Digitalmars-d-learn wrote:


 
 class A {
   object foo;
 }
 
 and
 
 var a = new A();
 a.foo = any class...;
 
 does D have something like this or template parameters are used
 instead of?
D has void*. But if you have a class, it's probably better to use the universal base class Object, which is more type-safe. Alternatively, check out std.variant, which provides true dynamic typing as a tagged union. T -- "You know, maybe we don't *need* enemies." "Yeah, best friends are about all I can take." -- Calvin & Hobbes
Feb 05 2021
parent Jack <jckj33 gmail.com> writes:
Thanks for your answers guys!
Feb 07 2021