Is there a generic type such as void* or C#'s object?

Paul Backus snarwin at gmail.com
Sat Feb 6 02:11:45 UTC 2021


On Saturday, 6 February 2021 at 02:01:28 UTC, Jack wrote:
> in C/C++ you have void* and C#'s object, to create a variable 
> to hold a genetic type. So in C# you can do:
>
> 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


More information about the Digitalmars-d-learn mailing list