# Shouldn't isempty(nothing) return true?

**URL:** https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296
**Category:** General Usage
**Created:** [February 24, 2018, 4:08pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296 "2018-02-24T16:08:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 24, 2018, 4:08pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/1 "2018-02-24T16:08:56Z")

</div>

As subject says, instead of

```julia
isempty(nothing)
ERROR: MethodError: no method matching start(::Void)

```

---

<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: [February 24, 2018, 4:10pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/2 "2018-02-24T16:10:40Z")

</div>

`nothing` is not a container or an iterable, it is a value. While some languages conflate these things, Julia distinguishes the empty tuple `()`, the boolean `false`, and special values like `nothing` (which, are in fact, nothing special, just some conventions make them seem so, cf `Missings.missing`).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 24, 2018, 5:30pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/3 "2018-02-24T17:30:45Z")

</div>

2 posts were split to a new topic: [Rhetorical technique side discussion](https://discourse.julialang.org/t/rhetorical-technique-side-discussion/9299)

---

<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: [February 24, 2018, 4:46pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/4 "2018-02-24T16:46:28Z")

</div>

> [@joa-quim](#):
>
> hordes of new users that Julia is expecting to attract from other languages

I don’t know what “Julia” is expecting.

Personally, I hope that newcomers recognize the fact that languages are different, and when learning a new language, it is inevitable that some initial assumptions that are invalidated. Every single design choice invalidates someone’s expectations, so this is not a useful guideline. Other aspects, eg consistency and clarify, are more important.

> [@joa-quim](#):
>
> want to have a simple answer to question: does my variable contains something or not?

In this case is better to have a third answer, eg an error indicating that the question does not make sense. Only containers can contain things.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 24, 2018, 4:56pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/5 "2018-02-24T16:56:38Z")

</div>

> [@Tamas\_Papp](#):
>
> eg an error indicating that the question does not make sense.

Sorry wanting to know if a variable contains something or not does make a lot of sense me. Period, no more technical details. Is it empty or not?

I do have a solution for this but it’s my firm believe that Julia is making a big mistake making the life of users harder than it needs.

```julia
function isempty_(arg)
	if (arg == nothing)
		return true
	end
	empty = false
	try
		empty = isempty(arg)
	end
	return empty
end

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 24, 2018, 5:10pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/6 "2018-02-24T17:10:47Z")

</div>

Why does it make sense to ask if `nothing` is empty?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 24, 2018, 5:12pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/7 "2018-02-24T17:12:18Z")

</div>

Personally, I find the “box” mental model for variables to be wholly confusing and not helpful — especially for newcomers to the language. Julia’s variables don’t really “contain” values. They’re just names. That’s it.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 24, 2018, 5:13pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/8 "2018-02-24T17:13:28Z")

</div>

For example because `findfirst` may return a `nothing` and I find the most reasonable to ask

`isempty(findfirst(...))`

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 24, 2018, 5:16pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/9 "2018-02-24T17:16:08Z")

</div>

Why wouldn’t you write `findfirst(...) == nothing`?

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 24, 2018, 5:18pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/10 "2018-02-24T17:18:24Z")

</div>

And why wouldn’t `isempty(nothing)` give the obvious answer, which is `true`?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 24, 2018, 5:24pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/11 "2018-02-24T17:24:25Z")

</div>

If you want to know if a value is `nothing` the obvious way to ask that is to check if it’s equal to `nothing`. How is asking if it’s empty obvious? It seems equally possible that `nothing` would be a 1-element container containing only itself, which means that it’s not empty.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 24, 2018, 6:16pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/12 "2018-02-24T18:16:51Z")

</div>

One thing that could maybe be useful is some sort of trait that would tell whether I’m allowed to call `isempty` on a given type: say a function `iscontainer` that would specify whether the argument is a container and so if I can try checking if it has elements. Does something like that exist? It would be along the lines of the `Base.HasLength` style tools to know whether one can call `length` on some iterator, for example.

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [February 24, 2018, 7:50pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/13 "2018-02-24T19:50:45Z")

</div>

> [@joa-quim](#):
>
> And why wouldn’t isempty(nothing) give the obvious answer, which is true?

`isempty` is part of the extended iteration protocol. Now, `nothing` isn’t iterable, but if it was it would be a scalar for all intents and purposes. Scalars iterate themselves and therefore they are not empty.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 24, 2018, 8:07pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/14 "2018-02-24T20:07:19Z")

</div>

> [@mbauman](#):
>
> Personally, I find the “box” mental model for variables to be wholly confusing and not helpful — especially for newcomers to the language. Julia’s variables don’t really “contain” values. They’re just names. That’s it.

In that case it seems a bit odd to me that

```julia
julia> isempty(3)
false

```

If anything, shouldn’t `isempty(nothing)` be `false`?

---

<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: [February 24, 2018, 8:13pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/15 "2018-02-24T20:13:20Z")

</div>

See  
[https://github.com/JuliaLang/julia/issues/7903](https://github.com/JuliaLang/julia/issues/7903)  
[https://github.com/JuliaLang/julia/pull/19700](https://github.com/JuliaLang/julia/pull/19700)

IMO this is indeed a wart, but opinions are divided on this.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [February 24, 2018, 8:41pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/16 "2018-02-24T20:41:33Z")

</div>

I have to say that I’m not a huge fan of `Number`s being iterable either. Even leaving aside whether this makes sense from a mathematical perspective, it seems like a ripe opportunity for creating code with unintended behavior.

I think the reason we may be stuck with it forever is that expressions like `1 .+ 1` are just too convenient. It makes it far easier to make all sorts of code generic and I have found myself using it rather often.

As far as `nothing` goes, one might think of `Number`, `Missing` and `Nothing` as implementing some sort of “4-valued logic” where the default value for `missing` is `missing` and the default value for `nothing` is “throw error”. I think I’m ok with that.

To whatever extent it makes sense for `nothing` to have container-like properties, it definitely seems that, in light of the above `isempty(nothing)` should return `false`, if anything. Would `isempty([nothing])` be `true`? What about `isempty([nothing, nothing])`? `isempty(nothing)` being `true` just seems totally inconsistent.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 24, 2018, 8:42pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/17 "2018-02-24T20:42:01Z")

</div>

This is the kind of feature request that sounds great until you actually use it. It’s like the mass of things in Python that are false.

[http://www.thomas-cokelaer.info/tutorials/python/boolean.html#what-is-false](http://www.thomas-cokelaer.info/tutorials/python/boolean.html#what-is-false)

You start by going “shouldn’t the empty string mean the same thing as false? Then you’d be able to do `if str` and it would just work because it returns false. Isn’t that intuitive?”. Then you start doing the MATLAB “wouldn’t it be nice if vectors were just 1-column matrices, and then you’d be able to easily switch between the two ideas? Aren’t they intuitively just a 1-column matrix?”.

These things are false equivalences, usually based on an isomorphism. The empty string is in some sense “false” if you map the space of strings to the space of booleans. It is the identity element and all of that. But that’s not the same thing as it actually being false.

The vote is always for simple… until it hurts you. `if str` is great, until `str` was accidentally an empty array instead of a string… and your program still ran! Isn’t it nice it ran? No, because the output is wrong, and now you have no idea everything keeps running perfectly fine but spits out non-sense. This is why in software engineering communities the lack of type-safety in languages like Python and Javascript are seen as such a huge issue: it naturally causes bugs which not even the runtime can find. The only way to find it is a human or a test checking the output!

Since safety is becoming such a big deal in programming (Rust’s key feature is that it’s “safe”, Haskell is all about type-safety), wouldn’t hordes of programmers want to flock to a language with type-safety? The issue has always been that this type of feature is stuck is the realm of statically compiled languages, so there hasn’t been a good answer. But Julia is dynamic, interpreted, and has key elements of type-safety (checked mostly at runtime, but maybe static checks could be in the future?). That’s a great feature to pitch!

This kind of feature is then against one of the big reasons to use Julia. `isempty` is part of the iteration protocol. Why not pun it to mean `false` on some non-iterables like `nothing`? The reason of course is that it will cause code that was meant for iterables to silently work and throw mysterious values on non-iterables. This is bad for scaling your code, like really really bad!

Lastly, in Julia its really not a big deal. Why not use:

```julia
x == nothing || isempty(x)

```

? Since `nothing` is a singleton of type `Void`, if your code is type stable the `x==nothing` part is computed at compile-time (if type-stable), and thus it doesn’t even have a runtime cost. So it’s a free computation that you can use to explicitly say you’re including `Void` with iterables. If you really want to save characters, just define `isnempty`.

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [February 24, 2018, 10:02pm UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/18 "2018-02-24T22:02:52Z")

</div>

I think that joa-quim expect more compact protocol from `findfirst`.

You could do

```julia
if contains(A, k) 
  r = findfirst(k, A)
end

```

but I think it has better performance if you just call `findfirst` and check result if it found anything.

But `findfirst` seems to be bad designed because it has problem with `Dict(nothing=>1)`, findnext for Dict has not meaning and there is not simple function (joa-quim expected something like `isempty`) to check return value if something was found.

BTW:  
`findfirst` on Set is not defined although [i for i in Set] is doable.

Analyzing it I found interesting error message:

```julia
julia> next(Set([1,2,3]),17)
ERROR: BoundsError: attempt to access 16-element Array{Int64,1} at index [17]

```

---

<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: [February 25, 2018, 1:01am UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/19 "2018-02-25T01:01:52Z")

</div>

I am constantly getting annoyed that I can’t just half-ass my code like I can in python by asking `if myvariable` and have it pretend to be false for empty strings, empty arrays, regexes that don’t match etc.

But once I actually make it work, I’m usually grateful in hindsight that the language forced me to be more clear and explicit, and it’s (usually) not even that much more verbose.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [February 25, 2018, 3:13am UTC](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296/20 "2018-02-25T03:13:30Z")

</div>

You can make a version of isempty return true for voids if you like  
I had a similar trouble with isnumber(type numeric)

```julia
module xmodifier
  function isempty(x::Void)
    return(true)
    end;

  function isempty(x)
    return(Base.isempty(x));
    end;
  end;

```

```julia
xmodifier.isempty(nothing)
> true

```

[Next page](https://discourse.julialang.org/t/shouldnt-isempty-nothing-return-true/9296.md?page=2)
