Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml: Type Checking Objects

If I have an object, how can I determine its type? (Is there an OCaml equivalent to Java's instanceof operator?)

like image 739
Nick Heiner Avatar asked Sep 15 '09 18:09

Nick Heiner


People also ask

Is OCaml object oriented?

Objects and classes. OCaml is an object-oriented, imperative, functional programming language :-) It mixes all these paradigms and lets you use the most appropriate (or most familiar) programming paradigm for the task at hand.

How does OCaml type inference work?

The OCaml type reconstruction algorithm attempts to never reject a program that could type check, if the programmer had written down types. It also attempts never to accept a program that cannot possibly type check.

What is type A in OCaml?

The type 'a is a type variable, and stands for any given type. The reason why sort can apply to lists of any type is that the comparisons (=, <=, etc.) are polymorphic in OCaml: they operate between any two values of the same type. This makes sort itself polymorphic over all list types.

What does well typed mean in OCaml?

The OCaml type system is also substantially more precise that than the type systems of C or Java. In order for an expression to be well-typed, the types expected by functions and operators must coincide with the types of their arguments.


3 Answers

OCaml has structural typing for objects rather than nominative typing as in Java. So the type of an object is basically determined (and only determined) by its methods. Objects in OCaml can be created directly, without going through something like a class.

You can write functions which require that its argument objects have certain methods (and that those methods have certain types); for example, the following method takes an argument that is any object with a method "bar":

let foo x = x#bar
like image 176
newacct Avatar answered Nov 29 '22 02:11

newacct


There's a discussion of "Matching Objects With Patterns" on Lambda the Ultimate (the paper uses Scala as the language, so won't answer your question). A more relevant Ocaml mailing list thread indicates that there's no RTTI/safe-downcasting for objects.

For algebraic (non object) types you obviously have:

match expr with 
  Type1 x -> x
  Type2 (x,y) -> y

called (pattern) matching

Someone did write an extension that allows down/up-casting Ocaml objects.

like image 40
Jonathan Graehl Avatar answered Nov 29 '22 02:11

Jonathan Graehl


In short, you have to encode your own RTTI mechanism. OCaml provides no RTTI or up/down casting (the latter in part because inheritance and subtyping are orthogonal in OCaml rather than unified as in Java).

You could do something with strings or polymorphic variants to encode type information in your classes and objects. I believe that LablGTK does some of this, and provides a utility library to support object tagging and up/down casting.

like image 36
Michael Ekstrand Avatar answered Nov 29 '22 04:11

Michael Ekstrand