www.digitalmars.com         C & C++   DMDScript  

D - more templates

reply "Carlos Santander B." <carlos8294 msn.com> writes:
Let's say I have something like this:

class A {}
class B:A {}
class C:A {}
template Z (T:B) { void bar() {} }
template Z (T:C) { void bar() {} }

And for some reason I need something like this:

void foo(A a) { instance Z ( a.type ).bar(); }
...
B b;
C c;
foo(b);
foo(c);

How could it be done? Currently, I don't think there's a way to do so, but I
think it could be useful. I'm not sure either about the "a.type" part.
There's no way to get the class of an object right now, right? Do you guys
think it could be useful? Is there another way to do this?

-------------------------
Carlos Santander


---

Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 2003-08-19
Aug 25 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:biel4j$4qu$1 digitaldaemon.com...
 Let's say I have something like this:

 class A {}
 class B:A {}
 class C:A {}
 template Z (T:B) { void bar() {} }
 template Z (T:C) { void bar() {} }

 And for some reason I need something like this:

 void foo(A a) { instance Z ( a.type ).bar(); }
 ...
 B b;
 C c;
 foo(b);
 foo(c);

 How could it be done? Currently, I don't think there's a way to do so, but
I
 think it could be useful. I'm not sure either about the "a.type" part.
 There's no way to get the class of an object right now, right? Do you guys
 think it could be useful? Is there another way to do this?
afaik D only supports template instatiation at compile time (this would require compile time template instantation [again D is a statically typed lang]) however Delphi solves this problem with class virtual functions (there is a static vtbl per class and you can pass types about as params so the above can be done with the following pseudo code class A { static virtual bar() { ... } } class B : A { static virtual override bar(){ ... } } void foo( A a ) { a.class.bar() } I assume your actual example is a little more complex, cos all you are doing in the above example is virtual calls implemented insome bizzare way (kiss!)
Aug 27 2003
parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bij1fr$t84$1 digitaldaemon.com...
|
| afaik D only supports template instatiation at compile time (this would
| require compile time template instantation [again D is a statically typed
| lang])

Runtime instantiation, I assume. Or am I missing something? But yes, I think
that doesn't help here.

|
| > class A {}
| > class B:A {}
| > class C:A {}
| > template Z (T:B) { void bar() {} }
| > template Z (T:C) { void bar() {} }
|
| however Delphi solves this problem with class virtual functions
| (there is a static vtbl per class and you can pass types about as params
| so the above can be done with the following pseudo code
| class A { static virtual bar() { ... } }
| class B : A { static virtual override bar(){ ... } }
| void foo( A a ) { a.class.bar() }
|

Something I forgot to mention: I can't modify the classes. Something you
didn't notice: there's no bar for A.
Something I should've added in the example:
template Z (T:A) { compileTimeError("can't instantiate Z with A's"); }
(following the notation proposed in another thread)

| I assume your actual example is a little more complex, cos all you are
doing
| in the above example is virtual calls implemented insome bizzare way
(kiss!)
|

Yes, it's more complex.

—————————————————————————
Carlos Santander


---

Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 2003-08-19
Aug 27 2003