-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstanceof.as
More file actions
58 lines (50 loc) · 1.46 KB
/
instanceof.as
File metadata and controls
58 lines (50 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// `instanceof` — runtime type test against a class (SP2 §1).
//
// `x instanceof C` is a comparison-tier binary operator yielding a bool: true iff
// `x` is an instance of `C` or any subclass of `C` (the `extends` chain is walked).
// A non-instance left operand (number, string, nil, …) is always false, never an
// error. The right operand must be a class.
class Animal {
fn speak() {
return "..."
}
}
class Dog extends Animal {
fn speak() {
return "woof"
}
}
class Cat extends Animal {
fn speak() {
return "meow"
}
}
fn describe(x) {
// Dispatch on the most specific class first, then the base class.
if (x instanceof Dog) {
return "a dog says " + x.speak()
}
if (x instanceof Cat) {
return "a cat says " + x.speak()
}
if (x instanceof Animal) {
return "some animal says " + x.speak()
}
return "not an animal"
}
let d = Dog()
let c = Cat()
let a = Animal()
print(describe(d)) // a dog says woof
print(describe(c)) // a cat says meow
print(describe(a)) // some animal says ...
print(describe(42)) // not an animal
// A subclass instance is `instanceof` its parent, but not the other way around.
print(d instanceof Animal) // true
print(a instanceof Dog) // false
// Non-instances are always false.
print("hello" instanceof Animal) // false
print(nil instanceof Animal) // false
// `instanceof` binds at the comparison tier, so it composes with `&&`.
print(d instanceof Animal && c instanceof Animal) // true
print("instanceof ok")