Determining a constant's value at compile time
Constants in Crystal are constant but not frozen. In other words, this means if you define a constant as an array, you would not be able to change its value to String
, but you could push/pop values to/from the array. This, coupled with macros being able to access the constant's value, lead to a fairly common practice of using macros to mutate constants at compile time so that the values could later be used/iterated over in a finished
hook.
With the introduction of annotations, this pattern is no longer as useful as it once was. However, it can still be helpful when you want to allow the user to be able to influence some aspect of your macro logic and there is no place to apply an annotation. One of the main benefits of this approach is that it can be called anywhere within the source code and still be applied, unlike annotations, which need to be applied to a related item.
For example, say we wanted to have a way to register...