www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to say to compiler that I want to inherit final template bethod of

reply "Uranuz" <neuranuz gmail.com> writes:
The question is in the header:
How to say to compiler that I want to inherit final template 
bethod of base interface into derived class?

I have the following example. I know that it is maybe 
overcomplicated but still I need this feature in my code.

import std.stdio;

interface IBase
{
	
	template getStr(string fieldName)
	{
		final string getStr()
		{
			return "George";
		}
	}
	
	
	string getStr(string fieldName);
}


class Derived: IBase
{
	override string getStr(string fieldName)
	{
		return "Sam";
	}
	
}


void main()
{
	auto obj = new Derived;
	
	writeln( obj.getStr!("aaa")() );
	
}

Compilation output:
/d907/f266.d(33): Error: obj.getStr isn't a template


When I have all of these methods having the same name compiler 
shadows template method from the base interface. So the question 
is how to inherit template method without reimplementing it in 
derived class?
Jul 20 2014
parent reply "anonymous" <anonymous example.com> writes:
import std.stdio;

interface IBase
{
	
	template getStr(string fieldName)
	{
		final string getStr()
		{
			return "George";
		}
	}
	
	
	string getStr(string fieldName);
}


class Derived: IBase
{
	alias getStr = IBase.getStr; /* order matters, see below */
	override string getStr(string fieldName)
	{
		return "Sam";
	}
	/* alias getStr = IBase.getStr; /* doesn't work here, I guess
that's a compiler bug */
}


void main()
{
	auto obj = new Derived;
	
	assert( obj.getStr!("aaa")() == "George" );
	assert( obj.getStr("aaa") == "Sam" );
	
}
Jul 20 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Sunday, 20 July 2014 at 12:48:09 UTC, anonymous wrote:
 import std.stdio;

 interface IBase
 {
 	
 	template getStr(string fieldName)
 	{
 		final string getStr()
 		{
 			return "George";
 		}
 	}
 	
 	
 	string getStr(string fieldName);
 }


 class Derived: IBase
 {
 	alias getStr = IBase.getStr; /* order matters, see below */
 	override string getStr(string fieldName)
 	{
 		return "Sam";
 	}
 	/* alias getStr = IBase.getStr; /* doesn't work here, I guess
 that's a compiler bug */
 }


 void main()
 {
 	auto obj = new Derived;
 	
 	assert( obj.getStr!("aaa")() == "George" );
 	assert( obj.getStr("aaa") == "Sam" );
 	
 }
Sorry, but this example doesn't work too.
Jul 20 2014
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 20 July 2014 at 15:48:19 UTC, Uranuz wrote:
 Sorry, but this example doesn't work too.
Ugh, 2.065 doesn't like it, but it works for me with git head (v2.066-devel-82b031c).
Jul 20 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Sunday, 20 July 2014 at 16:12:20 UTC, anonymous wrote:
 On Sunday, 20 July 2014 at 15:48:19 UTC, Uranuz wrote:
 Sorry, but this example doesn't work too.
Ugh, 2.065 doesn't like it, but it works for me with git head (v2.066-devel-82b031c).
Where did you get it? Or you compiled it yourself? Because I tried beta4 and it doesn't work there too.
Jul 20 2014
parent "anonymous" <anonymous example.com> writes:
On Sunday, 20 July 2014 at 16:47:30 UTC, Uranuz wrote:
 Where did you get it? Or you compiled it yourself?
I'm building it myself. It's not difficult, when you know basic git. And it doesn't take long. You can find instructions here: http://wiki.dlang.org/Building_DMD
 Because I tried beta4 and it doesn't work there too.
If it doesn't work in the 2.066 beta, I guess that means you'd have to wait for 2.067 to get the feature in a release.
Jul 20 2014