OCaml’s type system is awesome

…If not just a little bit complex. But for objects, it has some sort of crazy static duck typing thing going on.

let f obj = obj#set_x 4;;
(* f: <set_x: int -> 'a; ..> -> 'a *)

The bit I mean is the argument to f, i.e. the <set_x: int -> 'a; ..> part, which is, broadly speaking, “any object with a set_x function accepting an int“. It’s like C++ templates without all the excessive verbiage (or, alternatively, like a dynamic language).

let obj = object
    val mutable x: int = 0;
    method set_x x': unit = x <- x'
end;;
(* obj: <set_x: int -> unit> *)

Now of course we can apply f to obj and it will automagically work. Also, as you can see, we have singleton objects, which is nice I suppose?

Shame that the rest of the language isn’t so good. Well, I mean, compared to something like C of course it is, but it just feels like such a pain in the ass sometimes. (Why aren’t all my lets rec, and all that.)

In other news: the website’s up for paying again. Hopefully I’ll remember before it’s taken down this time.

Leave a Reply

You must be logged in to post a comment.