Language Design: Generics

Published on 2017-07-21. Last updated on 2022-07-30

Two interconnected design decisions achieve a particularly interesting sweet-spot in language design:

  1. The ident: Type syntax allows consistent and straight-forward placement of generics, compared to languages which use Type ident1:

    Generics ([T]) always follow the name of a class or a method, both at the definition-site and at the use-site.

  2. A clearly defined use of brackets results in a more regular, easier to understand syntax that has superior readability compared to languages that use < and > for generics as well as for comparisons and bitshifts, or use [] to stand for operations on arrays2:

    [] encloses type parameters or type arguments
    () groups expressions, parameter/argument lists or tuples
    {} sequences statements or definitions

This means that generics do not need to be treated as an “advanced” language concept.

Instead, the mental model becomes so simple that every class or method can be thought of having …

  • zero or one parameter lists for types, followed by
  • zero or more parameter lists for values.

class Foo[T](let bar: String)
  fun foo[U] = ???

fun main()
  let instance = Foo[String]("abc")[Int64]

And that’s all there is to it!