I'm reading OCaml code where lots of records are defined. These records define functions for an interactive command line tool.
The type of these records is:
{
name : string ;
help : string ;
run : string list -> unit
}
where name
is the name of the command, help
one little line of help for the function, and run
a function that takes arguments and computes the result.
I would like to use the name
field inside the run
function. Is there a way to do that? Something like a self.name
?
The tool must support OCaml>4.00.1
.
Thanks
Yes, you can define a record recursively with the
rec
keyword, roughly speaking as long as all the fields are guaranteed to not involve an arbitrary computation. The following works:However, this doesn't work: