What is the correct way to use integers with Jane Street's Base?

I am trying to port some older codebase from Core to Base and I can’t seem to figure out how to properly use integer types.

The code I’m starting from is here: https://github.com/loxs/ocaml-parsing/blob/master/src/LexBuffer.ml

If I include this in the beginning of file:

    open Base
    open Base.Int

I get this error for line 65 in the original file: https://github.com/loxs/ocaml-parsing/blob/master/src/LexBuffer.ml#L65

 This expression has type Base.int Base.option = Base.int option
       but an expression was expected of type Base.Int.t = int

I tried playing with various things like using the Base.Int63 module, but whatever I do, I can’t seem to make it work.

The type of the equality operator that’s open in Base is int -> int -> bool, whereas core and the standard library by default use the polymorphic equality operator of type 'a -> 'a -> bool.

Instead of lexbuf.last_char = Some cr use Option.equal Int.equal lexbuf.last_char (Some cr).

1 Like

Wow, that was quite instructive, thank you!
Do you know why did they do this?

It’s because the polymorphic operators often do not correspond to what users expect, since they check for equality of the runtime structure of the values. They also throw runtime errors if users try to compare functions for equality.

You can find out more in these posts:

1 Like

See also the relevant section in Base’s readme.