# Using macros

**URL:** <https://discourse.julialang.org/t/using-macros/39656>\
**Category:** General Usage\
**Created:** [May 17, 2020, 8:14pm UTC](https://discourse.julialang.org/t/using-macros/39656 "2020-05-17T20:14:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nimpa](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nimpa](https://discourse.julialang.org/u/Nimpa)\
**Post date:** [May 17, 2020, 8:14pm UTC](https://discourse.julialang.org/t/using-macros/39656/1 "2020-05-17T20:14:21Z")

</div>

Hello, how can i write a macro that takes four argument , namely a Dict , a key ( of arbitaray type) , and two expressions such that if the Dict contains the key , its value is bound to the local variable it within the first experssion which is evaluated; if  
the key cannot be found, the second expression is evaluated.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 17, 2020, 8:16pm UTC](https://discourse.julialang.org/t/using-macros/39656/2 "2020-05-17T20:16:48Z")

</div>

If I understand your question correctly, you just need a function like this:

```julia
f(d, key, a, b) = (key ∈ d) ? a : b

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [May 18, 2020, 12:06am UTC](https://discourse.julialang.org/t/using-macros/39656/3 "2020-05-18T00:06:55Z")

</div>

Not sure I understand the question correctly either, but I’d rather think that this is the desired behavior:

```julia
function foo(dict, key, found, default)
    haskey(dict, key) ? found(dict[key]) : default()
end

```

```julia
julia> d = Dict(:x => 42)
Dict{Symbol,Int64} with 1 entry:
  :x => 42

# If the value exists, double it; otherwise return 0
julia> foo(d, :x, val->2val, ()->0)
84

julia> foo(d, :y, val->2val, ()->0)
0

```

If I’m correct and you’re indeed referring to [anaphoric macros](https://en.wikipedia.org/wiki/Anaphoric_macro), I don’t think they are considered very idiomatic in Julia. In cases like these, I think the idiomatic solution would be to use anonymous functions like in the proposal above.

In an ideal world, the arguments to `foo` would be to ordered in such a way that it could be called with a `do` block. But this is only practical when there is only one argument that is a function (in which case this argument must be placed first). See for example the following method of `get`:  
&nbsp;&nbsp;&nbsp;&nbsp;[`get(f::Function, collection, key)`](https://docs.julialang.org/en/v1/base/collections/#Base.get)

---

<div class="post-metadata">

**Author:** ![Nimpa](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nimpa](https://discourse.julialang.org/u/Nimpa)\
**Post date:** [May 20, 2020, 10:08pm UTC](https://discourse.julialang.org/t/using-macros/39656/4 "2020-05-20T22:08:22Z")

</div>

yea am referring to anaphoric macros

---

<div class="post-metadata">

**Author:** ![Nimpa](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nimpa](https://discourse.julialang.org/u/Nimpa)\
**Post date:** [May 20, 2020, 10:09pm UTC](https://discourse.julialang.org/t/using-macros/39656/5 "2020-05-20T22:09:21Z")

</div>

Anaphoric macros
