Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write a template type? #161

Open
singularitti opened this issue Apr 22, 2021 · 5 comments
Open

How to write a template type? #161

singularitti opened this issue Apr 22, 2021 · 5 comments

Comments

@singularitti
Copy link

singularitti commented Apr 22, 2021

I have several types, they have the same fieldnames, but different names & tags in an XML file, i.e.,

@aml struct R "PP_R"
    type::String, att"type"
    size::UInt, att"size"
    columns::UN{UInt}, att"columns"
    text::String, txt""
end

@aml struct Rab "PP_RAB"
    type::String, att"type"
    size::UInt, att"size"
    columns::UN{UInt}, att"columns"
    text::String, txt""
end

where the only differences between R & Rab are their name & tags. I want to define a template type DataSection so that the following code

@aml struct DataSection{T} "tag"
    type::String, att"type"
    size::UInt, att"size"
    columns::UN{UInt}, att"columns"
    text::String, txt""
end

@aml struct DataSection{:R} "PP_R"
    type::String, att"type"
    size::UInt, att"size"
    columns::UN{UInt}, att"columns"
    text::String, txt""
end

@aml struct DataSection{:Rab} "PP_RAB"
    type::String, att"type"
    size::UInt, att"size"
    columns::UN{UInt}, att"columns"
    text::String, txt""
end

is automatically generated.

I tried to write an @amltag macro but it doesn't work:

macro amltag(T, t)
    return quote
        @aml struct DataSection{$T} $$t
            type::String, att"type"
            size::UInt, att"size"
            columns::UN{UInt}, att"columns"
            text::String, txt""
        end
    end
end

julia> @macroexpand1 @amltag(:R, "PP_R")
quote
    #= REPL[67]:3 =#
    #= REPL[67]:3 =# @aml struct DataSection{:R} $ "PP_R"
            #= REPL[67]:4 =#
            (type::String, att"type")
            #= REPL[67]:5 =#
            (size::UInt, att"size")
            #= REPL[67]:6 =#
            (columns::UN{UInt}, att"columns")
            #= REPL[67]:7 =#
            (text::String, txt"")
        end
end

As you see, there is an extra $ before "PP_R". I don't know if it is Julia's parsing mechanism causes this. If I put $$t the next line, it reduces to "PP_R" immediately

macro amltag(T, t)
     return quote
         @aml struct DataSection{$T}
             $t
             type::String, att"type"
             size::UInt, att"size"
             columns::UN{UInt}, att"columns"
             text::String, txt""
         end
     end
end

julia> @macroexpand1 @amltag(:R, "PP_R")
quote
    #= REPL[5]:3 =#
    #= REPL[5]:3 =# @aml struct DataSection{:R}
            #= REPL[5]:4 =#
            "PP_R"
            #= REPL[5]:5 =#
            (type::String, att"type")
            #= REPL[5]:6 =#
            (size::UInt, att"size")
            #= REPL[5]:7 =#
            (columns::UN{UInt}, att"columns")
            #= REPL[5]:8 =#
            (text::String, txt"")
        end
end
@aminya
Copy link
Owner

aminya commented Apr 25, 2021

That extra $ was fixable somehow. I have forgotten how to do that.

@aminya
Copy link
Owner

aminya commented Apr 25, 2021

I think you can use this function to remove that extra $. This is a bug in Julia. I applied a similar workaround in CompileBot.jl

toplevel_string(expr::Expr) = Meta.parse(replace(string(expr), r"^begin([\s\S]*)end$"=>s"\1"))

@singularitti
Copy link
Author

singularitti commented Apr 25, 2021

I think you can use this function to remove that extra $. This is a bug in Julia. I applied a similar workaround in CompileBot.jl

toplevel_string(expr::Expr) = Meta.parse(replace(string(expr), r"^begin([\s\S]*)end$"=>s"\1"))

This is really a temporary workaround, could you please report this bug to the julia team? I'm afraid I am not able to make it clear to them.

@singularitti
Copy link
Author

singularitti commented Apr 25, 2021

Is it possible we move the XML tag (e.g., "PP_R") before struct? It solves the problem pretty fine:

julia> macro amltag(T, t)
            return quote
                @aml $t struct DataSection{$T}
                    type::String, att"type"
                    size::UInt, att"size"
                    columns::UN{UInt}, att"columns"
                    text::String, txt""
                end
            end
       end
@amltag (macro with 1 method)

julia> @macroexpand1 @amltag(:R, "PP_R")
quote
    #= REPL[3]:3 =#
    #= REPL[3]:3 =# @aml "PP_R" struct DataSection{:R}
            #= REPL[3]:5 =#
            (type::String, att"type")
            #= REPL[3]:6 =#
            (size::UInt, att"size")
            #= REPL[3]:7 =#
            (columns::UN{UInt}, att"columns")
            #= REPL[3]:8 =#
            (text::String, txt"")
        end
end

I know this is a breaking change, but can we leave it to AcuteML v1.0?

@aminya
Copy link
Owner

aminya commented Apr 25, 2021

Yeah, this is a breaking change, but I am fine with it if it solves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants