The Crystal Programming Language Forum

How to have the "any" type

Hi! I have used NodeJS for a long time, so I used to the “any” type… I just wondered if there is a way to have an “any” type in Crystal.

I have used: alias Any = String | Int32 | Float64 | Bool | Array(Any)

There is no such type.
Smaller unions are easier to deal with, no unions are of course even easier.
Note that in your alias example, it is generally better to avoid recursive aliases.
A struct can be used, see the JSON::Any implementation to have an example.

Where do you need it?

I’m writing the Crystal version of micromachine. And an array of blocks is used:

def on(key, &block)
    @callbacks[key] << block

But Crystal compiler forces me to define the @callbacks type. But, actually it could a Proc that receives anything.

I always find myself in stressful situations when I try to use Proc. :sob:

Can’t the proc type be Proc(Nil)?