# Is there a shorthand for "getfield if isdefined"?

**URL:** <https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 21, 2017, 9:37pm UTC](https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902 "2017-12-21T21:37:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 21, 2017, 9:37pm UTC](https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902/1 "2017-12-21T21:37:09Z")

</div>

For example,

```julia
work_module = Main
work_method = :push!

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

```

// i.e.

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

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

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 21, 2017, 10:12pm UTC](https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902/2 "2017-12-21T22:12:36Z")

</div>

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

My bad

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 3, 2018, 12:57am UTC](https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902/3 "2018-01-03T00:57:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [January 3, 2018, 1:01am UTC](https://discourse.julialang.org/t/is-there-a-shorthand-for-getfield-if-isdefined/7902/4 "2018-01-03T01:01:42Z")

</div>

`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)
