Skip to content

Instantly share code, notes, and snippets.

@carlobaldassi
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlobaldassi/9609450 to your computer and use it in GitHub Desktop.
Save carlobaldassi/9609450 to your computer and use it in GitHub Desktop.
Simplified invoke interface via macro
macro invoke(ex)
Meta.isexpr(ex, :call) || error("invoke macro syntax error")
isa(ex.args[1], Symbol) || error("invoke macro syntax error")
fname = ex.args[1]
types = [Meta.isexpr(a, :(::)) ? a.args[2] : Expr(:call, :typeof, a) for a in ex.args[2:end]]
args = [Meta.isexpr(a, :(::)) ? a.args[1] : a for a in ex.args[2:end]]
Expr(:call, :invoke, fname, Expr(:tuple, types...), args...)
end
julia> using Images
julia> b = randbool(5,5);
julia> i = Image(randbool(5,5));
julia> i .* b
Binary Image with:
data: 5x5 BitArray{2}
properties:
limits: (false,true)
julia> @invoke i .* b
Binary Image with:
data: 5x5 BitArray{2}
properties:
limits: (false,true)
julia> @invoke i::AbstractArray .* b
5x5 Array{Bool,2}:
false true false false false
false true false true true
false true false true false
false false false false false
false true false true false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment