Question

While researching about structural typing I found the following post describing how interfaces in Go are translated to method lookup tables at runtime. The process described in the post seems vastly different than the reflective and generative techniques described for Scala's optional structural type system and for the White Oak extension of the Java language.

Are there any in-depth resources that discuss how structural typing is implemented in OCaml? I am particularly interested in any discussion of optimizations or run-time performance comparisons with nominal type systems.

Was it helpful?

Solution

You can find a rather detailed description of OCaml object internals in this blog post by Jake Donham. The gist of it is that object support is mostly implemented as an internal library, with only a bit of logic in the compiler itself (and of course object typing logic in the type system), mostly around efficient message dispatch.

I'm not an expert on this part of the language, but after a cursory inspection, it looks like OCaml relies on method lookups in a sorted method type (resolved to slots in the method table), with caching for the method called last, and optimization of statically known calls, in particular self calls inside method implementations. Finally, some commonly used functions (for example instance variable getters and setters) are recognized and encoded specifically (type impl in the internal OO library), to improve performances and, probably more importantly, reduce code size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top