user2524261
user2524261

Reputation: 109

What does this ocaml syntax mean?

type 'k leaf = {a_bb : 'k -> string;}

I'm unable to understand what this line of code in ocaml means. Can anyone help?

Upvotes: 0

Views: 258

Answers (1)

gallais
gallais

Reputation: 12093

It declares a new type leaf parametrised over a type variable 'k which is a record type with one field a_bb of type 'k -> string where 'k is the parameter we mentioned before.

An example of a value of type 'k leaf would be:

{ a_bb = fun _ -> "Hello World!" }

But the 'k can also be specialised to a concrete type e.g.

{ a_bb = fun b -> if b then "Hello World!" else "Argh!" }

has the type bool leaf because the argument to the function in the field a_bb has to be a boolean for the expression if b then (...) to make sense.

You can access the function in the field a_bb by using a projection like so:

fun v -> v.a_bb

Upvotes: 6

Related Questions