# Usage rules for bare underscores

**URL:** https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858
**Category:** General Usage
**Tags:** question
**Created:** [September 17, 2019, 5:56pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858 "2019-09-17T17:56:37Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 17, 2019, 5:56pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/1 "2019-09-17T17:56:37Z")

</div>

I’ve seen a few examples where one can use a bare underscore in an expression, e.g.,

```julia
julia> map(_ -> "hello", 1:4)
4-element Array{String,1}:
 "hello"
 "hello"
 "hello"
 "hello"

julia> ["hello" for _ in 1:4]
4-element Array{String,1}:
 "hello"
 "hello"
 "hello"
 "hello"

julia> for _ in 1:4
           println("hello")
       end
hello
hello
hello
hello

```

But of course the following doesn’t work:

```julia
julia> _ = 2
2

julia> _
ERROR: all-underscore identifier used as rvalue

```

However, I haven’t seen anything in the docs that describes when you can use a bare underscore and when you can’t. Does anyone know where this is documented, or if it’s not documented, can someone spell out the rules regarding bare underscores in expressions?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 17, 2019, 6:09pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/2 "2019-09-17T18:09:55Z")

</div>

`_` is used when you syntactically need a variable, but never use it. This is because there are lots of times (like your examples above) where you want a loop, which syntactically would need a variable to be defined. The reason for this convention is that knowing that a variable won’t be used makes code easier to read often.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 17, 2019, 6:12pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/3 "2019-09-17T18:12:11Z")

</div>

> [@Oscar\_Smith](#):
>
> `_` is used when you syntactically need a variable, but never use it.

Yeah, I get that. My question is, exactly when does the Julia parser allow a bare underscore and when does it not? Perhaps there are some interesting use cases that I’m missing! 🙂

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 17, 2019, 6:18pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/4 "2019-09-17T18:18:47Z")

</div>

The error message gives a pretty decent description. You can never use `_` as an rvalue, ie as something that goes on the right half of an equal sign. [c++11 - What exactly is an R-Value in C++? - Stack Overflow](https://stackoverflow.com/questions/9406121/what-exactly-is-an-r-value-in-c#9406672) does a pretty good job explaining (for c, but the terminology is the same).

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 17, 2019, 7:40pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/5 "2019-09-17T19:40:53Z")

</div>

Thanks. That SO post is helpful. That makes sense for the array comprehension and the `for` loop, since those can be written using assignment syntax:

```julia
julia> ["hello" for _ = 1:4]
4-element Array{String,1}:
 "hello"
 "hello"
 "hello"
 "hello"

julia> for _ = 1:4
           println("hello")
       end
hello
hello
hello
hello

```

However, the anonymous function syntax is still a little hard to understand:

```julia
julia> _ -> "hello"
#31 (generic function with 1 method)

```

Why is the underscore in that example considered an lvalue?

---

<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: [September 17, 2019, 7:51pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/6 "2019-09-17T19:51:58Z")

</div>

> [@CameronBieganek](#):
>
> Why is the underscore in that example considered an lvalue?

You don’t ever use its value. By using the name `_` for an argument, you are saying “I will never use this”.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 17, 2019, 8:07pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/7 "2019-09-17T20:07:05Z")

</div>

> [@StefanKarpinski](#):
>
> You don’t ever use its value.

To be honest, I don’t see how that has anything to do with whether something is an lvalue. Presumably, in the expression `x -> 2x`, the `x` on the left is an lvalue, but it’s not because “You don’t ever use its value.” My guess here is that function arguments can be considered lvalues in some sense? Furthermore, is it true that a bare underscore can be used _anywhere_ that an lvalue can occur? Which begs the question, which expressions or parts of expressions in Julia are considered lvalues? The above SO post partially answers that question, but I think we’re still left with the question of what exactly counts as an lvalue _in Julia_ and what exactly counts as an rvalue.

EDIT: I don’t think the answer to these questions is particularly urgent or important, but I am curious…

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 17, 2019, 8:32pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/8 "2019-09-17T20:32:11Z")

</div>

when I write

```julia
f(x) = ... blah blah blah ... 

```

the variable `x` is an l-value because it get’s assigned to when I do a function call. That is, when I say `f(2)`, a function scope will be created and inside that scope, there will be an assignment `x = 2`. So long as I don’t use `x` inside the function body, it’s perfectly legitimite to write

```julia
f(_) = ... blah blah blah ... 

```

instead.

* * *

Edit: r-value → l-value

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 17, 2019, 8:49pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/9 "2019-09-17T20:49:08Z")

</div>

Thanks! That makes sense. Though I’m guessing you meant “the variable `x` is an l-value”.

---

<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 18, 2019, 6:15am UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/10 "2019-09-18T06:15:23Z")

</div>

> [@CameronBieganek](#):
>
> My guess here is that function arguments can be considered lvalues in some sense?

Yes, you can think of them as being assigned the arguments when the function is called.

Whether something is an lvalue or not is a syntactic property orthogonal to being `_`.

Personally, I don’t think in terms of lvalues/rvalues for `_` (that’s the compiler’s job 😉). I would just go with this simple heuristic: if syntax requires a variable which you would otherwise not use, feel free to rename it `_`.

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [September 25, 2019, 7:49pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/11 "2019-09-25T19:49:42Z")

</div>

I’m wondering about that as well—when does the _Julia parser_ allow a bare underscore.

I have found an example that (I think) is not explained by what has been discussed so far, and it so happens it’s something I’d like to use in a module I’m working on.

Say a physical property c is usually a function some other T, i.e., c:c(T), but for a specific model `x`, c degenerates to a constant. As to prove a concept, I’m coding the model `x` as a custom type, say: `myModel`.

I’ve defined a working `c(x::myModel)` which returns the proper constant, but for the sake of interface, I’d like to have a `c(x::myModel, _) = c(x)` version of it—the `_` argument would be the other T that will never be used.

As for the breaking example, my original `c(x::myModel)` accepts keyword args. And I’ve found out that the existence of keyword arguments causes the julia-1.2.0 parser to complain about the `_` argument:

A minimum example to illustrate keyword args causes complaints with `_` args follows:

```julia
julia> c(x) = 3x # Fallback version (just a silly example)
c (generic function with 1 method)

julia> c(x, _) = c(x) # This works fine
c (generic function with 2 methods)

julia> c(x, _; double::Bool=false) = double ? 2c(x) : c(x) # _ is never used but parser complains
ERROR: syntax: all-underscore identifier used as rvalue around REPL[21]:1
Stacktrace:
 [1] top-level scope at REPL[21]:1

julia> c(x, _; double::Bool) = double ? 2c(x) : c(x) # This also fails
ERROR: syntax: all-underscore identifier used as rvalue around REPL[22]:1
Stacktrace:
 [1] top-level scope at REPL[22]:1

```

I’m wondering whether the existence of keyword args invalidating the use of `_` arguments might be a bug.

**EDIT** : I’ve found out that `c(x, ::T_Type; whatever)` works fine and solves the case issue; however, it doesn’t answer OP’s question.

---

<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 26, 2019, 6:09am UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/12 "2019-09-26T06:09:42Z")

</div>

Yes, you are running into the bug

[https://github.com/JuliaLang/julia/issues/32727](https://github.com/JuliaLang/julia/issues/32727)

---

<div class="post-metadata">

### Author: ![cnaak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnaak/32/10287_2.png) [@cnaak](https://discourse.julialang.org/u/cnaak)
#### Post date: [September 27, 2019, 3:53am UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/13 "2019-09-27T03:53:10Z")

</div>

Oh, yes. Thank you!!

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 20, 2022, 11:58pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/14 "2022-04-20T23:58:35Z")

</div>

For any beginners who may find this post, I just wanna point out that the underscore can also be used as a [digit grouping delimiter](https://en.wikipedia.org/wiki/Decimal_separator#Digit_grouping), to help readers visually parse big numbers. For example:

```julia
julia> 10_000 + 20_000
30000

```

Or you may prefer using the underscore to make it _harder_ for people to read your code:

```julia
julia> 3_4.5_6_7_890_123 + 0_1
35.567890123

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 9, 2024, 6:21pm UTC](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/15 "2024-02-09T18:21:23Z")

</div>

2 posts were split to a new topic: [Underscore syntax and anonymous modules?](https://discourse.julialang.org/t/underscore-syntax-and-anonymous-modules/110003)
