5.3 · Specialisation, witness tables, and opaque vs existential in the compiled code

Generics and protocols look like type-system features. At runtime they are two different calling strategies, and you can see which one you got.

Mental model. An unspecialised generic function receives the type and its protocol conformances as hidden arguments. Specialisation removes those arguments by copying the function for one concrete type.

Two ways to compile a generic

Hidden arguments versus a copy per type.

  1. How can you see that a generic function was specialised, and what does it depend on? 5.3.1 · L5
    A generic function can be compiled once, working on any type through hidden metadata, or specialised: the optimiser copies the body for a known type (say Int)…
  2. How does an unspecialised generic function know the concrete type, and what does that cost? 5.3.2 · L5
    A generic function’s machine code cannot know the size, copy rules or operations of T. So the caller passes extra hidden arguments: the type metadata for T (a…

some versus any in the output

The same protocol, two very different calls.

  1. What is different in the compiled code between returning some P and any P? 5.3.3 · L5
    Both hide a concrete type from the caller’s source, but the compiler treats them very differently. With some P the function’s return type is one specific type…

More in Compiler & language internals