Skip to main content
gamedev.net
🔒 Locked

Type generalization

Started by ToohrVyk Jul 7, 2006 at 5:52 AM 0 replies 400+ views
Original Post
ToohrVyk
ToohrVyk
I am defining a simple class, as follows:
type 'a tree =
  | Nil 
  | Leaf of 'a
  | Node of 'a tree array;;
  
class ['a] radix_map = object (self)
  val mutable size = 0
  val mutable tree : 'a tree = Node (Array.make 4 (Nil:'a tree))   
  method remove k = 
    let rec remove s = function
      | Nil -> ( )
      | Leaf _ -> size <- size - 1
      | Node a -> let (i,s) = index (k,s) in
          remove s a.(i);
          if s < 0 || a.(i) = Node empty then a.(i) <- Nil
    in remove (int_bits - 2) tree
end
The errors are:
The type of this class,
class ['a] radix_map :
  object
    val mutable size : int
    val mutable tree : '_a tree
    method remove : int -> unit
  end,
contains type variables that cannot be generalized
So, in effect, OCaml ignores both my type constraints that Nil and tree should be of type 'a tree (note that the 'a type is not free, it is bound by the class definition), and replaces it with a weak free type '_a. Why, and how can I go around this? EDIT: my stupidity knows no bounds. The error is that Empty is defined outside the class. However, the Why? question still stands: Why does O'Caml ignore my type constraints?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.