www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template interface and class

reply gerleim <elf_qt _deletethisifyouarenotaspammer_yahoo.com> writes:
The following will result in
"class test.C1 interface function I1.Foo isn't implemented"

class C1 : I1
{}
public interface I1
{
	void Foo();
}

, but 
class C(T) : I!(T)
{}

public interface I(T)
{
	T Foo();
}

will give compile error only if a declaration of the class is present somewhere.

Is this normal?
May 29 2007
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
gerleim wrote:
 The following will result in
 "class test.C1 interface function I1.Foo isn't implemented"
 
 class C1 : I1
 {}
 public interface I1
 {
 	void Foo();
 }
 
 , but 
 class C(T) : I!(T)
 {}
 
 public interface I(T)
 {
 	T Foo();
 }
 
 will give compile error only if a declaration of the class is present
somewhere.
 
 Is this normal?

Yes. Class templates exist only at compile-time. The compiler can't really determine if the class satisfies the interface until you instantiate the template. Consider: interface I { int foo(); } class C(T) : I { T foo(); } Does C implement I? It /might/, if T == int. The compiler can't tell until you instantiate the template. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
May 29 2007
parent reply gerleim <elf_qt _deletethisifyouarenotaspammer_yahoo.com> writes:
Kirk McDonald Wrote:

 gerleim wrote:
 The following will result in
 "class test.C1 interface function I1.Foo isn't implemented"
 
 class C1 : I1
 {}
 public interface I1
 {
 	void Foo();
 }
 
 , but 
 class C(T) : I!(T)
 {}
 
 public interface I(T)
 {
 	T Foo();
 }
 
 will give compile error only if a declaration of the class is present
somewhere.
 
 Is this normal?

Yes. Class templates exist only at compile-time. The compiler can't really determine if the class satisfies the interface until you instantiate the template. Consider: interface I { int foo(); } class C(T) : I { T foo(); } Does C implement I? It /might/, if T == int. The compiler can't tell until you instantiate the template. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org

Thanks for the answer, but I'm not fully convinced. A better example: class C(T) : I!(T) { } public interface I(T) { void Foo(); } Aside from the template it is sure that C misses implementation. That is also sure from my first example. That's not decided what type is T, but a method must be present with the name Foo. gerleim
May 30 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message 
news:f3klfg$nrq$1 digitalmars.com...
 Thanks for the answer, but I'm not fully convinced. A better example:

 class C(T) : I!(T)
 { }

 public interface I(T)
 {
 void Foo();
 }

 Aside from the template it is sure that C misses implementation.
 That is also sure from my first example. That's not decided what type is 
 T, but a method must be present with the name Foo.

Until you instantiate the template, no class exists. Period. No class means no way to check if it's right. The compiler does not do semantic analysis on template contents until they're instantiated, because there is absolutely no way to know, without knowing the template parameters, whether the code inside is valid or not. Because just like with templated functions, templated classes are syntactic sugar. Your class declaration is really saying: template C(T) { class C : I!(T) { } } Now it's obvious that there is no class at top-level.
May 30 2007
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Jarrett Billingsley wrote:
 "gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message 
 news:f3klfg$nrq$1 digitalmars.com...
 Thanks for the answer, but I'm not fully convinced. A better example:

 class C(T) : I!(T)
 { }

 public interface I(T)
 {
 void Foo();
 }

 Aside from the template it is sure that C misses implementation.
 That is also sure from my first example. That's not decided what type is 
 T, but a method must be present with the name Foo.

Until you instantiate the template, no class exists. Period. No class means no way to check if it's right. The compiler does not do semantic analysis on template contents until they're instantiated, because there is absolutely no way to know, without knowing the template parameters, whether the code inside is valid or not. Because just like with templated functions, templated classes are syntactic sugar. Your class declaration is really saying: template C(T) { class C : I!(T) { } } Now it's obvious that there is no class at top-level.

Furthermore, even if the compiler went so far as to check whether C contained at least symbols corresponding to those in I, there could still be later specializations of I which would change the situation. There would then need to be a full lookup on I, late in the process -- the sort of thing best performed during template instantiation. -- Chris Nicholson-Sauls
May 30 2007
parent reply gerleim <elf_qt _deletethisifyouarenotaspammer_yahoo.com> writes:
Thanks for the answers.

I don't see a way in my example when class C doesn't have the void Foo() method.

My test and post is coming from my experience with c# where you get error
message at compile time, which I think is convenient.
Implicit function template instantiation is also very convenient - that's why
I'm trying to do it that way.

gerleim

Chris Nicholson-Sauls Wrote:
 Jarrett Billingsley wrote:
 "gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message 
 news:f3klfg$nrq$1 digitalmars.com...
 Thanks for the answer, but I'm not fully convinced. A better example:

 class C(T) : I!(T)
 { }

 public interface I(T)
 {
 void Foo();
 }

 Aside from the template it is sure that C misses implementation.
 That is also sure from my first example. That's not decided what type is 
 T, but a method must be present with the name Foo.

Until you instantiate the template, no class exists. Period. No class means no way to check if it's right. The compiler does not do semantic analysis on template contents until they're instantiated, because there is absolutely no way to know, without knowing the template parameters, whether the code inside is valid or not. Because just like with templated functions, templated classes are syntactic sugar. Your class declaration is really saying: template C(T) { class C : I!(T) { } } Now it's obvious that there is no class at top-level.

Furthermore, even if the compiler went so far as to check whether C contained at least symbols corresponding to those in I, there could still be later specializations of I which would change the situation. There would then need to be a full lookup on I, late in the process -- the sort of thing best performed during template instantiation. -- Chris Nicholson-Sauls

May 31 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message 
news:f3m346$2t2e$1 digitalmars.com...
 My test and post is coming from my experience with c# where you get error 
 message at compile time, which I think is convenient.

Two things: one, C#'s generics are far less powerful than templates, and don't have all the issues that D's templates have. It's probably much easier to detect this at declaration time in C# than in D. Additionally generics are second-class -- the class owns the generic in C#, rather than the other way around. Two, you _will_ get a compile time error message in D -- just as long as you instantiate the class template at least once, like C!(int) c;.
May 31 2007
prev sibling parent reply Jason House <jason.james.house gmail.com> writes:
gerleim wrote:
 The following will result in
 "class test.C1 interface function I1.Foo isn't implemented"
 
 class C1 : I1
 {}
 public interface I1
 {
 	void Foo();
 }
 
 , but 
 class C(T) : I!(T)
 {}
 
 public interface I(T)
 {
 	T Foo();
 }
 
 will give compile error only if a declaration of the class is present
somewhere.
 
 Is this normal?

On a side note... void bar (I!(int) i){ i.Foo() } will not work because templated functions are never virtual.
May 30 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Jason House wrote:
 gerleim wrote:

 public interface I(T)
 {
     T Foo();
 }


 
 On a side note... void bar (I!(int) i){ i.Foo() }
 will not work because templated functions are never virtual.

There are no templated functions in this case, just a method which happens to implement a signature specified by a templated interface. That does not make the function itself templated.
May 31 2007