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
[Caml-list] difficulties narrowing OO types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-11-15 (01:06)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] difficulties narrowing OO types
From: William Harold Newman <>

> OCaml seems generally reluctant to narrow OO types.
OCaml is reluctant to do anything unsafe.

> E.g., dynamically casting from a base class to a subclass is
> unsupported (though I've seen the hack to work around this in Jason
> Hickey's online introduction).
No surprise, this is unsafe. Work arounds are available, but they may
all fail at runtime. The technical reason for not having it as a
construct in the language is that checking casts for parametric
classes is much harder than for classes without parameters.

> And as far as I can tell, it's
> impossible for a method on a subclass to guarantee that it returns a
> narrower type than the method on the base class.

That's actually a quite different problem. Having a method in a
subclass return a narrower type is type-safe, and is allowed by OCaml
subtyping rules. Unfortunately, OCaml inheritance rules does not allow

[Snipped lots of goods reason why one would want to narrow methods in

> If there really are systematic obstacles to narrowing OO types in
> OCaml, not just beginner confusion on my part, is there some deep
> reason? Perhaps because it causes problems with type inference? At
> first I thought it might be because it tends to be strongly correlated
> with typecase operations which miss the point of OO, but that doesn't
> explain why I can't narrow the return type of a subclass method.

You pointed it: type inference. Inferring class types is already
pretty complicated, it would become nightmarish if method types can
change with inheritance. Type inference is based on unification, which
doesn't play well with subtyping.

Since the problem is with inheritance, and not with subtyping, there
are workarounds to define such classes. Basically the idea is to keep
the type of self as a parameter to the class.

class ['a] base_t ~pos ~friends =
    method pos : int = pos
    method friends : 'a list = friends

class base = [base] base_t

class ['a] extended_t ~pos ~friends ~name =
    inherit ['a] base_t ~pos ~friends
    method name : string = name

class extended = [extended] extended_t

Here are the types:
class ['a] base_t :
  pos:int ->
  friends:'a list -> object method friends : 'a list method pos : int end
class base : pos:int -> friends:base list -> [base] base_t
class ['a] extended_t :
  pos:int ->
  friends:'a list ->
  name:string ->
  object method friends : 'a list method name : string method pos : int end
class extended :
  pos:int -> friends:extended list -> name:string -> [extended] extended_t

The point is that extended is actually a subtype of base:
# let b = new base ~pos:1 ~friends:[(e : extended :> base)];;
val b : base = <obj>

Note that this only works as long as base has no contravariant
method. If base_t contained an add_friend : 'a -> unit method, then
subtyping would break.

It might be possible to add method narrowing in the language with a
special syntax, but again parametric classes are hard to handle.


Jacques Garrigue
Bug reports:  FAQ:
To unsubscribe, mail  Archives: