Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Class syntax questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: prevost@m...
Subject: Class syntax questions
When trying to write an .mli to embody the types of the DOM interface
the other day, I came across what seemed to be a strange wart in the
language spec.

Specifically, I was trying to do something like the following:

type foo_sum =
  | A of a
  | B of b

class type any =
    method to_sum : foo_sum
class type a =
    method a : int

class type b =
    method b : bool

(I suspect that the above thing, which acts sort of like "downcasting",
isn't strictly needed to do the things DOM lets you do, since it's also
provided a more reasonable mechanism for accessing children of
different types--but, I want to expose both mechanisms, as the spec
includes them.)

Obviously, the above requires that the types any, a, b, and foo_sum be
corecursive.  Unfortunately, there's no way to use "and" with a class
type and a normal type.  This means that you need to fall back on
writing things like:

type any = < to_sum : foo_sum >
and a = < a : int; to_sum : foo_sum >
and ...

since < .. > notation can't use inherit.  Not too much of a problem
until you're trying to implement something like DOM, which has some
fairly complex interfaces.

In fact, I fear I may have written types doing this which are over large
for the type inference mechanism to handle nicely.  Or maybe not.  IN
any case, the size of the type declarations makes it difficult to find
what O'Caml thinks is wrong.

Is there any way around this in the language, or was it simply