Next: , Previous: , Up: syntaxes   [Contents][Index]


5.6 Casting the type of expressions

It is sometimes useful to explicitly declare the type signature of an expression, so that the expander can perform more type checks and, maybe, optimisations. We might want to do it as an unsafe operation, without introducing run–time validation of values. For example, if we know that the result of an expression is a vector of 3 fixnums, we might write:

($vector-ref ?expr 1)

and everything is all right, but we are not specifying that the return value is a fixnum.

In these corner cases, we can use the syntax unsafe-cast-signature: at expand–time, it tags an expression as returning values of a specified type signature. So, in the above example, we could write:

(unsafe-cast-signature (<fixnum>)
  ($vector-ref ?expr 1))

We can easily check how unsafe-cast-signature works at the REPL:

vicare> (unsafe-cast-signature (<fixnum>) 123)
$1 = 123
vicare> (expansion-of (unsafe-cast-signature (<fixnum>) 123))
$1 = '123
vicare> (type-of (unsafe-cast-signature (<fixnum>) 123))
$1 = #[signature (<fixnum>)]

for a truly untyped expression:

vicare> (expansion-of (unsafe-cast-signature (<fixnum>) (read)))
$1 = ((primitive read))
vicare> (type-of (unsafe-cast-signature (<fixnum>) (read)))
$1 = #[signature (<fixnum>)]

if the type is incompatible, and we know it at expand–time:

vicare> (unsafe-cast-signature (<fixnum>) "ciao")
Unhandled exception
 Condition components:
   1. &who: unsafe-cast-signature
   2. &message: "expression type is incompatible with the requested tag"
   3. &syntax:
       form: #<syntax expr=(unsafe-cast-signature (<fixnum>) "ciao")>
       subform: #<syntax expr="ciao" mark*=(src)>
   4. &irritants: (#[signature (<string>)])

The following syntactic bindings are exported by the library (vicare).

Syntax: unsafe-cast-signature ?signature ?expr

Expand to the ?expr expression itself, but, in the expander, tag the expression as returning a tuple of values with type signature ?signature.


Next: , Previous: , Up: syntaxes   [Contents][Index]