Skip to content

Commit

Permalink
created function merge macro
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed May 18, 2018
1 parent 693eb56 commit 3f6f830
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/ForceImport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ForceImport
# This file is part of ForceImport.jl. It is licensed under the MIT license
# Copyright (C) 2018 Michael Reed

export @force
export @force, @merge

"""
Expand Down Expand Up @@ -42,6 +42,32 @@ macro port(use)
return Expr(:block,out...)
end

"""
@merge fun(x) = x
Automatically imports `fun` from its module if necessary for function definition.
"""
macro merge(expr)
if !( (expr.head == :function) | ( (expr.head == :(=)) &&
(typeof(expr.args[1]) == Expr) && (expr.args[1].head == :call) ) )
throw(error("ForceImport: $expr is not a function definition"))
end
fun = expr.args[1].args[1]
return Expr(:quote,quote
for name in names(current_module())
try
eval(ForceImport.imp($(string(fun)),name))
end
end
eval($(Expr(:quote,expr)))
end)
end

function imp(fun::String,name::Symbol)
:(Symbol($fun) names($name) && (import $name.$(Symbol(fun))))
end

__init__() = nothing

end # module

0 comments on commit 3f6f830

Please sign in to comment.