how can i use the derive-hierarchy at runtime for typechecking?

Downs default_357-line at yahoo.de
Thu Sep 20 14:37:25 PDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Just cast it.
If you have class A { } and class B : A { }
and A foo=new B;
then you can do cast(B) foo and it will be non-null.
On the other hand, if you do A bar=new A;
and you do cast(B) bar, it will be null.

Here's a little utility function for this
void ifIs(S, T)(S obj, void delegate(T) dg) {
  auto casted=cast(T) obj;
  if (casted) dg(casted);
}

Use it like thus

ifIs(foo, (B whee) { /* do stuff with whee */ });

Hope that answers your question.
 --downs/FeepingCreature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG8ugVpEPJRr05fBERAiazAKCn3BIgtWPuCPeONAgE4AZZZ5BPZwCgjwFh
GvztcB0FD5gA2U6sBA6pZsc=
=G05B
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list