Julia: add @callsuper macro

Created on 26 Aug 2016  路  3Comments  路  Source: JuliaLang/julia

As per https://github.com/JuliaLang/julia/pull/13123, we could add a @supercall macro that sweetens the interface to invoke and makes it more intuitive. My current implementation:

macro callsuper(ex)
    ex.head == :call || error("@invoke requires a call expression")
    args = ex.args[2:end]
    types = Symbol[]
    vals = Symbol[]
    blk = quote end
    for arg in args
            val = gensym()
            typ = gensym()
            push!(vals, val)
            push!(types, typ)
            if isa(arg,Expr) && arg.head == :(::) && length(arg.args) == 2
                push!(blk.args, :($typ = $(esc(arg.args[2]))))
                push!(blk.args, :($val = $(esc(arg.args[1]))::$typ))
            else
                push!(blk.args, :($val = $(esc(arg))))
                push!(blk.args, :($typ = typeof($val)))
            end
    end
    push!(blk.args, :(invoke($(esc(ex.args[1])), ($(types...),), $(vals...))))
    return blk
end

Support for keyword arguments could be taken from https://github.com/JuliaLang/julia/pull/11165.

design help wanted speculative

Most helpful comment

Or maybe @invoke foo(x, y::T) could turn into invoke(foo, (typeof(x), T), x,y), i.e. you could use typeasserts to indicate the desired call signature.

All 3 comments

do the types used in invoke actually have to be supertypes of the values provided?

Or maybe @invoke foo(x, y::T) could turn into invoke(foo, (typeof(x), T), x,y), i.e. you could use typeasserts to indicate the desired call signature.

As a new feature, can be considered post-1.0. Triage also feels it's unlikely we'll remove invoke for 1.0.

Was this page helpful?
0 / 5 - 0 ratings