Cocan's OCaml FAQ


Is it possible to write UTF-8 string literals in source files ?

Yes, you just need to UTF-8 encode your source file and everything will work as expected. By doing so you loose the ability to use ISO 8859-1 (latin1) characters for program identifiers, you have to restrict yourself to ASCII identifiers. Note also that most functions of the String module will not work as expected on UTF-8 encoded strings.

Error handling

What's the difference between Invalid_argument and Failure ?

Xavier Leroy wrote :

Invalid_argument is very much like a failed assertion: it indicates that something is wrong in the program itself, i.e. negative character positions in string functions. Most programs will not catch Invalid_argument, treating as a fatal error. Others will catch it, but only to enter a piece of generic "recover from unexpected error" code.

Failure, on the other hand, signals errors that can happen in normal runs of the code. For instance, you're converting a user-provided string to a number, and the string does not represent a number. It is expected that the client code catches Failure and recovers gracefully, e.g. by asking for the number again, or producing a precise "syntax error" message.

I recommend the use of Invalid_argument to report "should never happen" conditions at the boundary between library functions and user code. On the other hand, the "Failure" exception is a bit of a legacy from earlier designs [...] and often is not the best way to report "normal error" conditions: instead, you could consider defining your own exceptions [...] or even have your functions return "option" types instead of raising exceptions.

Standard library

What is the quality of Random's pseudo-random number generators (PNRGs) ?

Will Farr wrote that the generators pass the Diehard statistical tests.