Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structural typing implementation of OCaml, Scala, and Go

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.

like image 229
Syllepsis Avatar asked Mar 07 '13 22:03

Syllepsis


People also ask

Is Go structurally typed?

The language compiler interprets Mallard to be a Duck — because it has a quack function. And Dog is not a Duck — because Dog doesn't quack. GO is a Structural-typed language.

What is meant by structural typing?

A structural type system means that when comparing types, TypeScript only takes into account the members on the type. This is in contrast to nominal type systems, where you could create two types but could not assign them to each other.


1 Answers

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.

like image 184
gasche Avatar answered Sep 20 '22 16:09

gasche