Skip to content

Document/consistent handling of macro and escape (unescape?) #10887

@yuyichao

Description

@yuyichao

From the document, it is probably clear enough why are macros and esc useful. However, IMHO, it is not quite clear when it is necessary to use esc. It's true that most of the cases it's quite intuitive but I could not find anywhere a short summary of the rules that cover all cases.

There also doesn't seem to be a way to "unescape".

The script that shows my confusion (and why I think "unescape" is necessary) is here,

module A
macro test_mac()
    quote
        function f(v::T1)
            v::T1
        end
        function Module.func1(v::T1)
            v::T1
        end
        @inline function Module.func2(v::T1)
            v::T1
        end
        function f{T2}(v::T2)
            v::T2
        end
        function Module.func1{T2}(v::T2)
            v::T2
        end
        @inline function Module.func2{T2}(v::T2)
            v::T2
        end
    end
end
end

println(macroexpand(:(A.@test_mac)))

The output is

begin  # /home/yuyichao/tmp/macro_escape.jl, line 6:
    function #1#f(#2#v::A.T1) # /home/yuyichao/tmp/macro_escape.jl, line 7:
        #2#v::A.T1
    end # line 9:
    function A.Module.func1(#3#v::A.T1) # /home/yuyichao/tmp/macro_escape.jl, line 10:
        #3#v::A.T1
    end # line 12:
    function A.Module.func2(A.v::A.T1)
        $(Expr(:meta, :inline)) # /home/yuyichao/tmp/macro_escape.jl, line 13:
        A.v::A.T1
    end # line 15:
    function #1#f{#5#T2}(#4#v::#5#T2) # /home/yuyichao/tmp/macro_escape.jl, line 16:
        #4#v::#5#T2
    end # line 18:
    function A.Module.func1{#7#T2}(#6#v::#7#T2) # /home/yuyichao/tmp/macro_escape.jl, line 19:
        #6#v::#7#T2
    end # line 21:
    function A.Module.func2{A.T2}(A.v::A.T2)
        $(Expr(:meta, :inline)) # /home/yuyichao/tmp/macro_escape.jl, line 22:
        A.v::A.T2
    end
end

It would be nice to document why are f, v, T2 mangled but not T1 and Module.func1. Also, is there a way to make the inline function definition works as expected (unescape?)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions