# Propagating Nothing when a parameter is Nothing generically

**URL:** https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270
**Category:** New to Julia
**Tags:** question
**Created:** [September 16, 2021, 3:11pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270 "2021-09-16T15:11:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Bob2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bob2/32/29219_2.png) [@Bob2](https://discourse.julialang.org/u/Bob2)
#### Post date: [September 16, 2021, 3:11pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/1 "2021-09-16T15:11:03Z")

</div>

I want something like this for a collection of a couple dozen small functions.

```julia
f(a::Nothing, b) = nothing
f(a, b::Nothing) = nothing
f(a::Nothing, b::Nothing) = nothing

f(a, b) = a + b

```

But I wish to do so without adding that code to each of the few dozen actual implementations (eg: a + b) and without adding 3 additional methods.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 16, 2021, 3:23pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/2 "2021-09-16T15:23:53Z")

</div>

I believe if you write it like:

```julia
function f(a, b)
    (isnothing(a) || isnothing(b)) && return nothing
    ... do your normal thing...
end

```

the compiler will optimize it to equivalently what you wrote up there

EDIT: thanks to @Per for pointing out `|| &&` priority

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [September 16, 2021, 3:26pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/3 "2021-09-16T15:26:57Z")

</div>

This is a potentially good use case for a macro. You can end up with something like:

```julia
@propogate_nothing f(a, b) = a + b

```

And have it translate into:

```julia
f(a::Nothing, b) = nothing
f(a, b::Nothing) = nothing
f(a::Nothing, b::Nothing) = nothing
f(a, b) = a + b

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 16, 2021, 4:09pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/4 "2021-09-16T16:09:19Z")

</div>

> [@jling](#):
>
> I believe if you write it like:
> 
> …
> 
> compiler will optimize it to equivalently what you wrote up there

Yes, it will. Use cases like this is one time it is good idea to do type checking inside the body over dispatch.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [September 16, 2021, 4:24pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/5 "2021-09-16T16:24:08Z")

</div>

You will need parentheses:

```julia
(isnothing(a) || isnothing(b)) && return nothing

```

since `||` has lower priority than `&&`.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 16, 2021, 4:43pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/6 "2021-09-16T16:43:04Z")

</div>

You may be better off working with `missing`, instead of `nothing`. Unlike `nothing`, `missing` is designed to propagate, and has ready-made utilities to make propagation easier.

See `passmissing` from Missings.jl

```julia
help?> passmissing
search: passmissing

  passmissing(f)

  Return a function that returns missing if any of its positional
  arguments are missing (even if their number or type is not consistent
  with any of the methods defined for f) and otherwise applies f to these
  arguments.

```

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [September 16, 2021, 5:41pm UTC](https://discourse.julialang.org/t/propagating-nothing-when-a-parameter-is-nothing-generically/68270/7 "2021-09-16T17:41:50Z")

</div>

One of the conceptual distinctions between nothing and missing (imo) is that nothing is handled immediately (sometimes by converting to missing) or an error is thrown.  
Where as missing is propagated until it is handled (e.g. with imputation) or until it can’t be supported and then an error is thrown
