as

    In the above code, a is a union of Int32 | String. If for some reason we are sure a is an Int32 after the if, we can force the compiler to treat it like one:

    1. a_as_int = a.as(Int32)
    2. a_as_int.abs # works, compiler knows that a_as_int is Int32

    The as pseudo-method performs a runtime check: if wasn’t an Int32, an exception is raised.

    The argument to the expression is a .

    !!! note You can’t use as to convert a type to an unrelated type: as is not like a cast in other languages. Methods on integers, floats and chars are provided for these conversions. Alternatively, use pointer casts as explained below.

    The as pseudo-method also allows to cast between pointer types:

    1. ptr = Pointer(Int32).malloc(1)

    In this case, no runtime checks are done: pointers are unsafe and this type of casting is usually only needed in C bindings and low-level code.

    No runtime checks are performed in these cases because, again, pointers are involved. The need for this cast is even more rare than the previous one, but allows to implement some core types (like String) in Crystal itself, and it also allows passing a Reference type to C functions by casting it to a void pointer.

    The as pseudo-method can be used to cast an expression to a “bigger” type. For example:

    1. b = a.as(Int32 | Float64)
    2. b # :: Int32 | Float64

    The above might not seem to be useful, but it is when, for example, mapping an array of elements:

    Sometimes the compiler can’t infer the type of a block. This can happen in recursive calls that depend on each other. In those cases you can use as to let it know the type: