is_a - checks whether a val has a type that is a descendant of a type
oadl::is_a(
val,
typ)
The is_a()
intrinsic procedure checks to see
whether the value val has a type that is descended from the
given typ. For simple scalar types, this determination is
trivial. For objects and classes, this determination follows directly
from the inheritance chain. For array values, it is more complicated.
OADL implements an implicit inheritance tree for array types. At the
root of this tree is the type Array[*]
. The next
level of the tree consists of the generic array types as returned by
typeof()
- types such as
List
, Array
,
PackInt
, PackFloat
,
String
, and so on. The lowest level of the tree
consists of the specific array types as returned by the
parent()
method - types such as
List[3]
, String[10]
,
PackInt[3,4]
, and so on:
┌────┐ │Type│ └─┬──┘ ├───────┬─────────────┬─── ┄ ┌─┴─┐ ┌──┴──┐ ┌────┴───┐ │Int│ │Float│ │Array[*]│ ┄ └───┘ └─────┘ └────┬───┘ ┌────────────┬───┴────────┬─────────────┬───────────────┬─── ┄ ┌──┴─┐ ┌──┴──┐ ┌───┴──┐ ┌───┴───┐ ┌────┴────┐ │List│ │Array│ │String│ │PackInt│ │PackFloat│ ┄ └──┬─┘ └──┬──┘ └───┬──┘ └───┬───┘ └────┬────┘ ┌───┴───┐ ┌──────┴─────┐ ┌────┴────┐ ┌──────┴─────┐ ┌───────┴──────┐ │List[n]│ │Array[n,m,…]│ │String[n]│ │PackInt[n,…]│ │PackFloat[n,…]│ ┄ └───┬───┘ └──────┬─────┘ └────┬────┘ └──────┬─────┘ └───────┬──────┘ ┌─────┴────┐ ┌─────┴─────┐ ┌───┴───┐ ┌───┴───┐ ┌─────┴────┐ │{'a',1.,3}│ │new Array()│ │"hello"│ │[1,2,3]│ │[1.,2.,3.]│ ┄ └──────────┘ └───────────┘ └───────┘ └───────┘ └──────────┘
If typ is a parent type of val, returns
true
. Returns false
otherwise.
TypeCheck
if typ is not a
Type
class cls1 {} class cls2(cls1) {} cls2 obj() oadl::is_a(obj, cls2) true oadl::is_a(obj, cls1) true oadl::is_a("foobar", Array[*]) true oadl::is_a("foobar", String) true oadl::is_a("foobar", String[6]) true // The Array type is not the same as the Array[*] type oadl::is_a("foobar", Array) false // However, a generic 2x3 array of "nil" is an Array oadl::is_a(nil.reshape(2,3), Array) true