Skip to content

Commit

Permalink
Add @which macro
Browse files Browse the repository at this point in the history
To be used directly in front of a function call, e.g.:
@which rand(1:10)
@which 1 + im
@which a[1,2]
@which a[1:end] = 2
  • Loading branch information
carlobaldassi committed Feb 10, 2013
1 parent 5c9eb04 commit 85fdc92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ export
@time,
@timed,
@elapsed,
@which,
@windows_only,
@unix_only,
@osx_only,
Expand Down
22 changes: 22 additions & 0 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ end

which(f, args...) = whicht(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args))

macro which(ex)
ex = expand(ex)
exret = expr(:call, :error, "expression is not a function call")
if !isa(ex, Expr)
# do nothing -> error
elseif ex.head == :call
exret = expr(:call, :which, map(esc, ex.args)...)
elseif ex.head == :body
a1 = ex.args[1]
if isa(a1, Expr) && a1.head == :call
a11 = a1.args[1]
if isa(a11, TopNode) && a11.name == :assign
exret = expr(:call, :which, eval(expr(:toplevel, :assign)), map(esc, a1.args[2:end])...)
end
end
elseif ex.head == :thunk
exret = expr(:call, :error, "expression is not a function call, or is too complex for @which to analyze; "
* "break it down to simpler parts if possible")
end
exret
end

edit(file::String) = edit(file, 1)
function edit(file::String, line::Integer)
editor = get(ENV, "JULIA_EDITOR", "emacs")
Expand Down

0 comments on commit 85fdc92

Please sign in to comment.