Stream: helpdesk (published)

Topic: Expand Int in macro


view this post on Zulip Simon Christ (Feb 23 2021 at 22:43):

How can I do the transformation of :Int to the correct concrete type symbol in a macro?

view this post on Zulip Mason Protter (Feb 23 2021 at 22:47):

what do you mean by correct concrete type symbol?

view this post on Zulip Simon Christ (Feb 23 2021 at 22:54):

Depending on platform get :Int64 or :Int32 instead of :Int

view this post on Zulip Mason Protter (Feb 23 2021 at 22:57):

there is no correct symbol though.

julia> let Int32 = Base.Int64, Int64 = Base.Int32, Int = Float64
           typeof((Int32(1), Int64(2), Int(3)))
       end
Tuple{Int64, Int32, Float64}

view this post on Zulip Mason Protter (Feb 23 2021 at 23:20):

If you have the type itself, you could just do Symbol(T) to get it's symbol and that'll have the 64 or 32 in it.

julia> Symbol(Int)
:Int64

view this post on Zulip Simon Christ (Feb 24 2021 at 19:03):

That works! Thank you. For future reference:

julia> macro t(e)
         :(Tuple{$(Symbol(e))})
       end
@t (macro with 1 method)

julia> @t Int
Tuple{Int64}

view this post on Zulip Mason Protter (Feb 25 2021 at 17:57):

@Simon Christ I don't think that macro does what you think it does. The Symbol constructor isn't doing anything:

julia> macro t2(e)
         :(Tuple{$e})
       end
@t (macro with 1 method)

julia> @t Int
Tuple{Int64}

On 64bit computers, Int === Int64, whereas on 32 bit computers, Int === Int32. E.g. on my machine:

julia> Int
Int64

view this post on Zulip Simon Christ (Feb 25 2021 at 19:28):

Hmm.. seems my confusion was more about how to splat correctly:

julia> macro t(e)
         :(Tuple{$(e.args...)}) |> esc
       end
@t (macro with 1 method)

julia> @t [Int, Any]
Tuple{Int64,Any}

julia> macro t(e)
         :(Tuple{$(e.args)...}) |> esc
       end
@t (macro with 1 method)

julia> @t [Int, Any]
Tuple{:Int,:Any}

Last updated: Nov 06 2024 at 04:40 UTC