Skip to Content
Cloth

Any and Void

any and void are two special types that exist in their own categories in the type system. Each has a single, narrow purpose.

void

void denotes the absence of a value. It is legal only in one position: as the return type of a function that does not return a meaningful result.

public static func println() : void { OutputStream.$puts(""); }

A function whose return type is void either falls off the end of its body or executes a return statement with no operand. It cannot return a value, and its result cannot be assigned to a variable.

void is not a value type. It cannot appear as a parameter type, a field type, or an array element type. The expression void is not legal in any other position.

any

any is a type-erased value. A variable, parameter, or field of type any may hold a value of any other type. The original type information is preserved internally and can be recovered through a type check or cast.

The intended uses of any are narrow: heterogeneous collections at the boundary of the program, dynamic plugin protocols, and generic infrastructure that has no static knowledge of its payload. Inside ordinary code, prefer the most specific type the situation allows.

Recovering the underlying type

Two operators interrogate the runtime type of an any value:

  • is — a type-check expression. value is T evaluates to true when the runtime type of value is T (or a subtype of T).
  • as — a type cast. value as T produces a value of type T if the underlying type matches; the cast is checked, and a failure is a runtime error.

These operators are described in detail in Operators.

Comparison

TypeWhere legalHolds a value?
voidfunction return type onlyNo — there are no values of type void
anyany type positionYes — can hold a value of any other type

The two types are unrelated. They appear in this same page because they are the two special-form base types in the type system: every other base type is either named, generic, an array, or a tuple.