Overloading based on ‘var T’ / ‘out T’

If the formal parameter f is of type var T (or out T) in addition to the ordinary type checking, the argument is checked to be an l-value. var T (or out T) matches better than just T then.

  1. proc sayHi(x: int): string =
  2. # matches a non-var int
  3. result = $x
  4. proc sayHi(x: var int): string =
  5. # matches a var int
  6. result = $(x + 10)
  7. proc sayHello(x: int) =
  8. var m = x # a mutable version of x
  9. echo sayHi(x) # matches the non-var version of sayHi
  10. echo sayHi(m) # matches the var version of sayHi
  11. sayHello(3) # 3
  12. # 13

An l-value matches var T and out T equally well, hence the following is ambiguous:

  1. proc p(x: out string) = x = ""
  2. proc p(x: var string) = x = ""
  3. var v: string
  4. p(v) # ambiguous