Macro to create a function with a variable number of elseif branches

@kristoffer.carlsson beat me to it. Is there any practical difference between using elseif vs a series of &&?

macro custom_getproperty(name, symbolstuple...)
    methoddef = :(function Base.getproperty(obj::$name, s::Symbol) end)
    functionblock = last(methoddef.args).args

    for (i, sym) in enumerate(symbolstuple)
        if_case = :(s === $(symbolstuple[i]) && return getfield(obj, :data)[$i])
        push!(functionblock, if_case)
    end

    push!(functionblock, :(error("type $($name) has no field $s")))

    esc(methoddef)
end
2 Likes