query interface

Jarrett Billingsley jarrett.billingsley at gmail.com
Wed Jan 21 08:11:28 PST 2009


On Wed, Jan 21, 2009 at 10:30 AM, Qian Xu <quian.xu at stud.tu-ilmenau.de> wrote:
> Hi All,
>
> can D check, whether a class A an Interface B supports?
>
> like:
>
>  if (supports(class_A, intf_B))
>  {
>     cast(intf_B) (class_A).hello();
>  }

At compile time, like Frits said, you can use is(class_A : intf_B).

At runtime, you can use cast(intf_B)someInstance.  That returns 'null'
if someInstance does _not_ inherit from intf_B.

if(auto i = cast(intf_B)instanceOfA)
    i.hello();


More information about the Digitalmars-d-learn mailing list