typeof operator

Note: typeof(x) can for historical reasons also be written as type(x) but type(x) is discouraged.

One can obtain the type of a given expression by constructing a typeof value from it (in many other languages this is known as the typeof operator):

  1. var x = 0
  2. var y: typeof(x) # y has type int

If typeof is used to determine the result type of a proc/iterator/converter call c(X) (where X stands for a possibly empty list of arguments), the interpretation where c is an iterator is preferred over the other interpretations, but this behavior can be changed by passing typeOfProc as the second argument to typeof:

  1. iterator split(s: string): string = discard
  2. proc split(s: string): seq[string] = discard
  3. # since an iterator is the preferred interpretation, `y` has the type ``string``:
  4. assert typeof("a b c".split) is string
  5. assert typeof("a b c".split, typeOfProc) is seq[string]