vyrn

Protocols and generics

A protocol names methods a type can implement. A generic bounded by it is compiled once per concrete type, so dispatch costs nothing at run time.

Protocols and bounds

protocol declares the methods. impl P for T supplies them for one type, including a built-in type such as Int64.

A generic function takes a bound: fn label<T: Show>(x: T). The compiler generates one label per concrete type and calls the implementation directly. There is no table and no dynamic lookup: the choice is made once, while compiling.

A protocol can also declare an associated type. Chapter 11 uses one: Fallible owes an Output, which is what ? hands back.

[41] [yes]