passing subclass to superclass where parameter is a delegate for the superclass

Alex sascha.orlov at gmail.com
Wed Nov 14 17:26:52 UTC 2018


On Wednesday, 14 November 2018 at 16:39:52 UTC, Alex wrote:
>
> Are you looking for this?
> https://dlang.org/phobos/std_traits.html#TransitiveBaseTypeTuple
>
> It matches however not exactly your needs:
> As all objects are derived from the Object class, you will 
> always get it as the common parent. So... with the trait, you 
> get a list but have still to make a decision which type to use.

Maybe, something like this:

´´´
import std.traits;

template properBase(T)
{
	static foreach(i, t; TransitiveBaseTypeTuple!A)
	{
		static if(hasMember!(t, "add_function")){}
		else
		{
			static if(!__traits(compiles, {auto c = properBase.init;}))
			{
				alias properBase = TransitiveBaseTypeTuple!A[i-1];
			}
		}
	}
}

void main(){}

class base
{
     void delegate(typeof(this)) stored_dg;
	this(){}
     void
     add_function (void delegate (typeof(this)) dlg)
     {
         stored_dg = dlg;
     }
}

class A : base
{
	alias pB = properBase!A;
     this ()
     {
         super ();
         add_function (&this.foo);
     }

     void foo (pB a)
     {
		import std.experimental.logger;
         log ("i got here");
     }
}
´´´


More information about the Digitalmars-d-learn mailing list