Warning there was a bug inside the version 0.1c (see Changes). To correct it the new version 0.1d has several important changes.
"instance of" | "upcast" | "downcast" | |
Syntax | expr instanceof c | upcast var from c1 to c2 | downcast var from c1 to c2 |
Typing | bool | c2 | c2 |
Evaluation | true or false | value of var | value of var |
semantic | implementation | |
1. | o "is-a" c1 | ( Cast.check loc o key_c1) |
upcast o from c1 to c2
semantic | implementation | |
1. | o is exactly an instance of c1 and c1 inherits directly from c2 |
Cast.check_sonof var c1 c2 |
2. | type of var is a sub-type of c2 | ( var :> c2) |
downcast o from c1 to c2
semantic | implementation | |
1. | o has type c1 | ( o : c1) |
2. | c2 is a sub-type of c1 | ( c2 :> c1) |
3. | o "is-a" c1 | ( Cast.check loc o key_c1) |
4. | o "is-a" c2 | ( Cast.check loc o key_c2) | 5. | ((Obj.magic o) : c2) |
using "upcast" o from c2 to c1 (at one level of inheritence) to allow to downcast at any level or using o ":>" c1 to downcast to the class of construction, if you use ":>" one time, you can't downcast at any level!
z inherits from m m inherits from a z is sub-type of a m is sub-type of a Then we can write cast ((new z) :>a) from a to m that is wrong if z is not a sub-type of m For example m has a binary method which is not in a but which is inherited by z.
o instanceof cto check if an instance "is-a" c (its class is c or inherits from c).
ocaml : O'Caml 3.02
camlp4 : camlp4 3.02