Am Freitag, den 10.06.2016, 14:12 -0400 schrieb Hendrik Boom: > In http://caml.inria.fr/pub/docs/u3-ocaml/ocaml-objects.html it says: > > reverse coercions from a supertype to a supertype are never possible in > OCaml. > > (a) Might one of these 'supertype's actually be a 'subtype'? The second should be a subtype. > (b) Is there any way to do a run-time type-test on a value of a > specific statically known supertype to test whether it is actually a > value of a known specified subtype, and if so to proceed to use it as > that subtype? This is not possible (essentially because subtyping is not bound back to the class hierarchy (a subclass is not necessarily a subtype), and there is no runtime representation of the types). To some extent you can emulate what you want with open variants, e.g. (untested): type leaftype = .. class type supertype = object ... method leaftype : leaftype end class type subtype = ... type leaftype += | Subtype of subtype class subclass : subtype = object(self) inherit supertype method leaftype = Leaftype self ... end For every subclass, define a new variant for leaftype. Then, the test is let leafobj = match obj#leaftype with | Subtype x -> x | _ -> failwith "coercion failed" Gerd > If so, I'd like to use this in a site of pattern-directed rule > invocations. > > If not, I'd like to know if there are any well-known efficient ways > around this. Of course I can effectively Godel-encode my data > structures using lists, and interpret all my patterns and the > application types I woyd have rather expressed as OCaml types, but I'm > looking for something more elegant, statically checked, and > efficient. > > -- hendrik > -- ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de My OCaml site: http://www.camlcity.org Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de ------------------------------------------------------------