Template method in interfaces

Arafel via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 10 09:41:58 PDT 2016


On Wednesday, 10 August 2016 at 15:52:29 UTC, Lodovico Giaretta 
wrote:
> On Wednesday, 10 August 2016 at 15:48:10 UTC, Lodovico Giaretta 
> wrote:
>> On Wednesday, 10 August 2016 at 15:39:19 UTC, Arafel wrote:
>>> Would it even make sense to "force" (deprecation warning) a 
>>> "final" keyword in any implicitly-final function (I wasn't 
>>> even aware of those, I have to admit)? It would make things 
>>> much clearer, like with "override"...
>>
>> I read the spec again, and found out that it says interfaces 
>> cannot contain templated functions... So either my 
>> interpretation is the intended one and the spec is outdated, 
>> or the spec is right and the compiler is bugged.
>
> Anyway what I said about implicit final is true for classes. In 
> classes, I don't like the idea of having to put an explicit 
> final, but this is debatable. For interfaces, I'm ok with 
> forcing an explicit final attribute (but as I said the spec 
> does not allow templated functions in interfaces, even if the 
> compiler does).

I have to say that the fact that this compiles at all seems like 
a bug to me according to [1], even more so that the method in A 
is called:

---
import std.stdio;

public class A {
	public void func(T)(T t) {
		writeln("Within A");
	}
}

public class B : A {
	override public void func(T)(T t) {
		writeln("Within B");
	}
}

void main() {
	A a = new B();
	a.func(1);
}
---

https://dpaste.dzfl.pl/f3d5beff2e51

If the function is "final", even if implicitly so, the "override" 
should fail according to the spec as I, and I guess 99% of the 
people[2], understand it.

[1]: https://dlang.org/spec/function.html#virtual-functions
[2]: OK, technically not, since it just says that "Functions 
marked as final may not be overridden in a derived class [...]" 
and this function is not *marked* as final, but implicitly 
final... still...



More information about the Digitalmars-d-learn mailing list