Is there a shorthand for "getfield if isdefined"?

question

#1

For example,

work_module = Main
work_method = :push!

cur_var = 
  isdefined(work_module, work_method) ? 
  getfield(work_module, work_method) : 
  nothing

// i.e.

> println( softget(Main, :push!) )
push!

> println( softget(Main, :foo) )
nothing

#2

Guessing this is type unstable and that’s why it doesn’t exist.

My bad


#3

That’s not really a problem (isdefined can be inferred), but this is just not recommended code style. Why don’t you just pass the push! Function itself instead, instead of a binding name?


#4

push! was just an example. it could have been:

  • color
  • cur_database
  • etc.

// the use case for this is doing some map-reduce style operation while metaprogramming


update: another usecase is calling :head on AST transversals

(in order to sometimes ignore the difference between symbols and expressions)