TYPED:
Strongly-typed word definitions

Next:TYPED::


Vocabulary
typed

Syntax
TYPED: word ( a b: class ... -- x: class y ... ) body ;


Word description
Like :, defines a new word with a given stack effect in the current vocabulary. The inputs and outputs of the stack effect can additionally be given type annotations in the form a: class. When invoked, the word will attempt to coerce its input values to the declared input types before executing the body, throwing an input-mismatch-error if the types cannot be made to match. The word will likewise attempt to coerce its outputs to their declared types and throw an output-mismatch-error if the types cannot be made to match.

Notes
The aforementioned type conversions and checks are structured in such a way that they will be eliminated by the compiler if it can statically determine that the types of the inputs at a call site or of the outputs in the word definition are always correct.

Examples
A version of + specialized for floats, converting other real number types:
USING: math prettyprint typed ; IN: scratchpad TYPED: add-floats ( a: float b: float -- c: float ) + ; 1 2+1/2 add-floats .
3.5


See also
TYPED::, define-typed

Definition