Class syntax questions

From: prevost@maya.com
Date: Wed Jan 20 1999 - 21:03:09 MET


Message-Id: <199901202003.PAA19118@zarya.maya.com>
Date: Wed, 20 Jan 1999 15:03:09 -0500 (EST)
From: prevost@maya.com
Subject: Class syntax questions
To: caml-list@inria.fr

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 =
  object
    method to_sum : foo_sum
  end
  
class type a =
  object
    method a : int
  end

class type b =
  object
    method b : bool
  end

(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
overlooked?

John.



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:18 MET