Jump to content
  • Advertisement
Sign in to follow this  

Type generalization

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
The errors are:
The type of this class,
class ['a] radix_map :
    val mutable size : int
    val mutable tree : '_a tree
    method remove : int -> unit
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?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!