Search code examples
typescompiler-constructionocaml

How to handle multiple ocaml types in ocaml?


I'm building a compiler in Ocaml and I got a problem.. I've one variable called value and this variable should receive any kind of ocaml type(int, float, char or bool), but I can't do it..

type info = {  
  initialized: bool;
  mutable value : int;
}

I tried creating a new type like this:

type tipos = int 
           | float
           | char
           | bool 

and define info as:

type info = {  
  initialized: bool;
  mutable value : tipos;
}

but it still don't work..

Anyone can help me? thank you.


Solution

  • You need constructors for your different variants. Something like this will at least be a lot closer to working:

    type tipos = Int of int | Float of float | Char of char | Bool of bool
    

    Then values of the type look like this:

    # Int (3 * 5);;
    - : tipos = Int 15
    # Float (3.2 /. 2.0);;
    - : tipos = Float 1.6
    # Char 'a';;
    - : tipos = Char 'a'
    # Bool true;;
    - : tipos = Bool true