Annotation lik @foo_str but for numbers?

I can define a @foo_str macro, which lets me write foo"something". Is there an equivalent mechanism for numbers? The closest I can think if in Julia would be something like 2im, so this can probably be achieved using units, but I’m wondering about a more general solution.

Note that I don’t have a specific use case in mind, I was just wondering about, say, @matchbox_int to write code like matchbox20, which would for example generate bland post-grunge sounds.

No, there is nothing like string macros for numbers. im works by overloading *, since 1im is juxtaposed multiplication of 1 and im:

julia> @which 1im                                     
*(x::Real, z::Complex{Bool}) in Base at complex.jl:321

julia> @which 1*im                                     
*(x::Real, z::Complex{Bool}) in Base at complex.jl:321
                                                      
julia> typeof(im)                                     
Complex{Bool}                                         

You can use the same trick in your own code though (and I believe that’s how e.g. Unitful.jl does it as well). It also only works if the constant is written after the number literal, since otherwise it’s parsed as a single identifier:

julia> im1                           
ERROR: UndefVarError: im1 not defined
2 Likes