Vocabulary
typed

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


Word description
Like ::, defines a new word with named inputs 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 the quadratic formula specialized for floats, converting other real number types:
USING: kernel math math.libm prettyprint typed ; IN: scratchpad << TYPED:: quadratic-roots ( a: float b: float c: float -- q1: float q2: float ) b neg b sq 4.0 a * c * - fsqrt [ + ] [ - ] 2bi [ 2.0 a * / ] bi@ ; >> 1 0 -9/4 quadratic-roots [ . ] bi@
1.5 -1.5


See also
TYPED:, define-typed

Definition