Is there a way to use Object.factory with templated classes? Or some way to construct templated classes given RTTI of an instance?

Chad Joan chadjoan at gmail.com
Thu Sep 27 18:37:27 UTC 2018


On Thursday, 27 September 2018 at 08:56:22 UTC, Alex wrote:
> On Wednesday, 26 September 2018 at 20:41:38 UTC, Chad Joan 
> wrote:
>
>> class Root(T)
>> {
>> 	T x;
>> }
>>
>> class Extended(T) : Root!T
>> {
>> 	T y;
>> }
>>
>
> Sorry for a technical aside, but would this be something for 
> you?
> https://forum.dlang.org/post/vtaxcxpufrovwfrkbyye@forum.dlang.org
>
> I mean... In either case, there is something curious in the 
> Extended/Root usage, as they both are bound to the same type. 
> And if so, you could get rid of the templatization in the 
> Extended class, either by templating Root on the Extended or 
> use the T of Root in Extended.

Perhaps it's half some form of unintended neural network 
fabrication that happened as I wrote the example and half that 
the original code isn't that well thought out yet.  The original 
code looks more like this:

template Nodes(T)
{
	class Root
	{
		T x;
	}

	class Extended : Root
	{
		T y;
	}
}

I will probably end up going with the latter suggestion and have 
Extended use the Root's T.  That would probably make sense for 
what I'm doing.  In my case, the T allows the caller to configure 
what kind of output the thing provides... IIRC (it's been a while 
since I touched this code o.O).

Thanks for pointing that out.


More information about the Digitalmars-d-learn mailing list