Is there a shorthand for "getfield if isdefined"?



For example,

work_module = Main
work_method = :push!

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

// i.e.

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

> println( softget(Main, :foo) )


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

My bad


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?


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)