# Best way to make "present?" function in Julia?

**URL:** https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394
**Category:** General Usage
**Tags:** question
**Created:** [September 1, 2018, 3:38am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394 "2018-09-01T03:38:15Z")
**Posts on this page:** 10
**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: [September 1, 2018, 3:38am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/1 "2018-09-01T03:38:15Z")

</div>

Rails has this function called `.present?` that really helps with one-liners

// i.e. is something not nothing, nil, missing, zilch, nada

What is the best way to replicate this behavior in Julia?

> <https://stackoverflow.com/questions/13186722/what-is-the-difference-between-using-exists-and-present-in-ruby/13186788#13186788>

* * *

**edit:** this is kind of what I’m working with now:

```julia
is_nothing(cur_object) = false
is_nothing(cur_object::Void) = true
is_nothing(cur_object::Number) = iszero(cur_object)

is_present(cur_object) = !is_nothing(cur_object)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 1, 2018, 5:27am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/2 "2018-09-01T05:27:13Z")

</div>

`nothing`, `missing`, and numbers `==0` have different semantics in Julia (which is a good thing). What you are asking for deviates from standard semantics, so you have to make your own choices and code them accordingly. The function above is a good way of doing this.

That said (and I hesitate to say this without more context) doing something like this in Julia is [code smell](https://en.wikipedia.org/wiki/Code_smell).

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [September 1, 2018, 7:14am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/3 "2018-09-01T07:14:38Z")

</div>

@djsegal I recommend **not** considering typed zeros as unpresent values. The existence of an additive identity (we call that `zero` for Integers and some other structures), lets someone develop cascading and interacting logic that becomes this computer.

This should confer an awareness of the nonpresent.

```julia
function ispresent(x::T) where {P, T<:Union{Missing, Nothing, P}}
    !(iismissing(x) || isnothing(x))
end

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 1, 2018, 8:01am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/4 "2018-09-01T08:01:05Z")

</div>

I don’t see the need for types above, I think that

```julia
ispresent(x) = x ≢ nothing && x ≢ missing

```

should be equivalent.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [September 1, 2018, 8:39am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/5 "2018-09-01T08:39:48Z")

</div>

Yes, I use type annotations that not required as documentation to remind myself what containers of xs types’ should be.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 1, 2018, 8:58am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/6 "2018-09-01T08:58:00Z")

</div>

> [@JeffreySarnoff](#):
>
> For additional performance at the margin

I don’t think this has any performance impact, I would be surprised if it did not compile to essentially the same code. I just like `≡` & friends visually, `ismissing` is fine.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [September 1, 2018, 9:16am UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/7 "2018-09-01T09:16:21Z")

</div>

I find your syntax easier to read, too.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 1, 2018, 1:26pm UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/8 "2018-09-01T13:26:59Z")

</div>

Just replying to remind myself to come back and explore what this weird equality operator is 🙂

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 1, 2018, 1:39pm UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/9 "2018-09-01T13:39:27Z")

</div>

Nothing weird about it, just Unicode aliases:

```julia
julia> ≡
=== (built-in function)

julia> ≢
!== (generic function with 1 method)

```

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 1, 2018, 1:42pm UTC](https://discourse.julialang.org/t/best-way-to-make-present-function-in-julia/14394/10 "2018-09-01T13:42:16Z")

</div>

> [@Tamas\_Papp](#):
>
> Nothing weird about it

I meant “Weird” in the sense of “I’ve never seen it before,” not “this is abhorrent to the natural order” 😆. Thanks for explaining though so I don’t have to go digging once I get back to a computer 🙂
