Search code examples
d

Is instance castable to class


class A { this() { } }
class B : A { this() { } }
auto b = new B()
if(b.IsCastableTo(A)) {
    //...
}

How would I check if b can be casted to type A?


Solution

  • According to the D manual you would do:

    if (cast(A) b) { 
      // b is an instance of A 
    } else { 
      // b is not an instance of A 
    }
    

    References: