Shorhand for printing the content and also the type of a varoible?

While trying to learn Crystal I keep printing the type of variables for which I write p typeof(x).
Is there a shorter way to do it?

I created a macro for this:

macro t(name)
  print "%s %s\n" % {typeof({{name}}), {{name}}}
end

Is it possible to tell Crystal to load this macro for every Crystal program?
Basically to require a file without any require statement? Sort of like .bashrc for the shell?

There’s currently no way to do that.

I usually pp! value, typeof(value), value.class To have both the compiler type and the runtime type.

To make something implicitly available the only workaround is to define a custom prelude (that will require the std-prelude). But that means passing —prelude myprelude to all crystal cli invocations, or defining CRYSTAL_OPTS env variable.

3 Likes