Question: What is the most elegant way to do this:

BCS ao at pathlink.com
Fri Aug 10 10:04:16 PDT 2007


Reply to Daniel919,

> Hi, I attached my solution.
> 

I guess that works. (<delets file> Excuse my while I autoclave my computer).

how abou this:


|import std.stdio;
|
|template check(T...)
|{
|  void check(Tx)(Tx x, void delegate()[] dg ...)
|  {
|    static if (is(Tx == T[0]))
|    {
|      dg[0]();
|    }
|    else
|    {
|      foreach(i, Ts; T[1..$])
|      {
|        if (cast(T[i])x !is null)
|        {
|          dg[i]();
|          return;
|        }
|        throw new Exception("cast error");
|      }
|    }
|  }
|}
|
|class TypeA0 { int id = 10; }
|class TypeA : TypeA0 { int id = 11; }
|class TypeA2 : TypeA { int id = 12; }
|class TypeB { int id = 20; }
|
|void main()
|{
|  auto x = new TypeA;
|  check!(TypeA, TypeB)(x, {writefln("TypeA id=%s", x.id);}, {writefln("TypeB id=%s", x.id);});
|}



> Best regards,
> Daniel


More information about the Digitalmars-d-learn mailing list