digitalmars.D.learn - Template interface and class
- gerleim (17/17) May 29 2007 The following will result in
- Kirk McDonald (17/39) May 29 2007 Yes. Class templates exist only at compile-time. The compiler can't
- gerleim (11/55) May 30 2007 Thanks for the answer, but I'm not fully convinced. A better example:
- Jarrett Billingsley (14/24) May 30 2007 Until you instantiate the template, no class exists. Period. No class
- Chris Nicholson-Sauls (6/38) May 30 2007 Furthermore, even if the compiler went so far as to check whether C cont...
- gerleim (6/46) May 31 2007 Thanks for the answers.
- Jarrett Billingsley (9/11) May 31 2007 Two things: one, C#'s generics are far less powerful than templates, and...
- Jason House (3/25) May 30 2007 On a side note... void bar (I!(int) i){ i.Foo() }
- Frits van Bommel (6/14) May 31 2007 [snip]
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
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
Kirk McDonald Wrote:gerleim wrote: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. gerleimThe 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 30 2007
"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
Jarrett Billingsley wrote:"gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message news:f3klfg$nrq$1 digitalmars.com...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-SaulsThanks 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
Thanks for the answers. I don't see a way in my example when class C doesn't have the void Foo() method. 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...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-SaulsThanks 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 31 2007
"gerleim" <elf_qt _deletethisifyouarenotaspammer_yahoo.com> wrote in message news:f3m346$2t2e$1 digitalmars.com...message at compile time, which I think is convenient.don't have all the issues that D's templates have. It's probably much 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
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
Jason House wrote:gerleim wrote:[snip][snip]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