Skip to content

49 Generic inference ​

Entry<> infers both type arguments from named constructor values. choose infers T from its arguments without spelling it at the call site.

norm
value Entry<T, U> {
  T first
  U second
}

T choose<T>(T left, T right) { return left }

main() {
  var entry = Entry<>(first: 7, second: "seven")
  var selected = choose(left: entry.second, right: "other")
  printLine(entry.first)
  printLine(selected)
}

Expected output:

text
7
seven

Try it: Change the entry's second value and the right argument to integers, then inspect the type of selected.

Precise rules: Reference.

Norm 0.25