# A most harrowing collection of Julia WATs

**URL:** <https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323>\
**Category:** General Usage\
**Tags:** wat\
**Created:** [March 2, 2022, 6:15pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323 "2022-03-02T18:15:32Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [March 7, 2022, 8:31am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/21 "2022-03-07T08:31:34Z")

</div>

Same for me, but my brain wasn’t so fond about the same thing happening to `Float32`s:

```julia
julia> f = 10
10

julia> 2f + 1
21

julia> 2*f+1
21

julia> 2f+1
20.0f0

julia> 2f+1f # (2f+1)*f
200.0f0

```

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

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [March 7, 2022, 8:54am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/22 "2022-03-07T08:54:15Z")

</div>

This was definitely wat for me, and is arguably a bug:

```julia
julia> 0.0 in Set([-0.0])
false

julia> 0.0 in collect(Set([-0.0]))
true

```

And on the flip side:

```julia
julia> NaN in Set([NaN])
true

julia> NaN in [NaN]
false

```

Edit: I didn’t read the docs for `in` closely enough: It’s not a bug, but it definitely is wat

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 7, 2022, 9:14am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/23 "2022-03-07T09:14:50Z")

</div>

~~`0.0 in Set([-0.0])` is definitely a bug in the implementation (or the documentation) as [Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Base.in) says “For example, Sets check whether the item isequal to one of the elements.”~~ (Edit: this is incorrect)

By the way, the specification of `Base.in` is clearly qualified as a WAT: `Base.in` may follow `==` or `isequal` semantics depending on the argument types.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 7, 2022, 9:34am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/24 "2022-03-07T09:34:14Z")

</div>

> [@tkf](#):
>
> `0.0 in Set([-0.0])` is definitely a bug in the implementation

This is documented the way it works, as the documentation states:

> Some collections follow a slightly different definition. For example, [`Set`](https://docs.julialang.org/en/v1/base/collections/#Base.Set)s check whether the item [`isequal`](https://docs.julialang.org/en/v1/base/base/#Base.isequal) to one of the elements.

and

```julia
julia> 0.0 == -0.0
true

julia> isequal(0.0, -0.0)
false

```

but

> By the way, the specification of `Base.in` is clearly qualified as a WAT

Fully agreed

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 7, 2022, 9:34am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/25 "2022-03-07T09:34:18Z")

</div>

To go with your other example of lowering iteration specifications… Here’s the weirdest way I know to define a closure:

```julia
julia> for wat() in 1:3
           println(wat())
       end
1
2
3

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 7, 2022, 9:43am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/26 "2022-03-07T09:43:07Z")

</div>

> [@bkamins](#):
>
> ```julia
> julia> 0.0 == -0.0
> true
> 
> julia> isequal(0.0, -0.0)
> false
> 
> ```

Thanks, I did check it, and it is definitely in the REPL history. I have no idea why I interpreted it backward.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 7, 2022, 9:43am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/27 "2022-03-07T09:43:19Z")

</div>

Who knew we had infix macros?

```julia
julia> macro +(a,b)
           QuoteNode((a,b))
       end
@+ (macro with 1 method)

julia> @(x + y*2)
(:x, :(y * 2))

```

(Please don’t use this! I hesitate to even mention it and I think it should get removed soon.)

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 8, 2022, 1:29am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/28 "2022-03-08T01:29:02Z")

</div>

@miguelraz the categorization of the `countlines()` example in your Julia WATs seems wrong to me. I’d class it as “paths are hard”, perhaps. See also

[https://github.com/JuliaLang/Juleps/pull/26](https://github.com/JuliaLang/Juleps/pull/26)

---

<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:** [March 8, 2022, 3:25am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/29 "2022-03-08T03:25:19Z")

</div>

Using `isequal` is the only reasonable behavior, using `==` is quite broken.

---

<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:** [March 8, 2022, 3:25am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/30 "2022-03-08T03:25:58Z")

</div>

Wow, this is wild.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 9, 2022, 12:09am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/31 "2022-03-09T00:09:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [March 9, 2022, 4:21pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/32 "2022-03-09T16:21:15Z")

</div>

> Lowering is hard, credit to `Jonnie Diegelman` :
> 
> ```julia
> julia> nums = zeros(Int, 10);
> 
> julia> for nums[rand(1:10)] in 1:20
> end
> 
> julia> nums
> 10-element Vector{Int64}:
> 12
> 16
> 7
> 20
> 19
> 18
> 15
> 0
> 13
> 17
> 
> ```
> 
> (Python suffers from something [similar](https://twitter.com/nedbat/status/1498426481906786305?s=20&t=1fmfRPR3rpzGVp2y4_vMoQ)). Explanation: As `Jabon Nissen` pointed out, “It’s because for i in 1:20 lowers to for i = 1:20 in Julia. Here, it’s nums[rand(1:10)] = 1:20”

I do find this behavior very surprising, but I don’t think it has anything to do with lowering. It’s equally surprising to me with `=` as with `in` (after all, `nums[rand(1:10)] = 1:20` is not a valid assignment anyway).

Maybe an example like this helps explain what is going on:

```julia
julia> x = [0,0]
2-element Vector{Int64}:
 0
 0

julia> for x[1] in 1:2; println(x); end
[1, 0]
[2, 0]

```

or the example by [Chris\_Foster](https://discourse.julialang.org/u/Chris_Foster) above?

I wonder if this was deliberately implemented or is just a bug, since it doesn’t seem to be mentioned in the docs ([Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops)). In fact, the docs seem to imply that the `for` line always defines new variables. Also, it doesn’t work with `let`:

```julia
julia> let x[1] = 1; end
ERROR: syntax: invalid let syntax around REPL[28]:1

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 9, 2022, 6:09pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/33 "2022-03-09T18:09:04Z")

</div>

> In fact, the docs seem to imply that the for line always defines new variables.

The linked documentation seems very clear to me:

> [Here the 1:5 is a range object, representing the sequence of numbers 1, 2, 3, 4, 5. The for loop iterates through these values, assigning each one in turn to the variable i. One rather important distinction between the previous while loop form and the for loop form is the scope during which the variable is visible. If the variable i has not been introduced in another scope, in the for loop form, it is visible only inside of the for loop, and not outside/afterwards.](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops)

Maybe the term variable is inaccurate, as the `for` loop clearly lowers the code in a way that `setindex` is called, but I either expected this to be an error or to work the way it works (i.e. working like `x[1] = iteration_value` was called at the start of the loop each iteration).

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 9, 2022, 6:49pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/34 "2022-03-09T18:49:01Z")

</div>

A lot of this is just people omitting spaces where they really should not omit spaces. Use a linter or a coding style or something…

It’s equivalent to writing

```julia
f(x) = x
    + 1

```

and then wondering why `f(1) == 1`.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [March 9, 2022, 7:10pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/35 "2022-03-09T19:10:43Z")

</div>

A good source of wats is the `where` keyword:

```julia
julia> isa isa where where where
true

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [March 9, 2022, 7:46pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/36 "2022-03-09T19:46:34Z")

</div>

> [@Henrique\_Becker](#):
>
> The linked documentation seems very clear to me:

It’s very clear only about the little it says, which is only about variables. And to me, the fact that it consistently says “variable” and not “expression” suggests that it only works for variables. And the part about scope seems to say that the variables to the left of the `=` or `in` are _new_ variables (although technically the paragraph only covers the case where there is no existing variable of the same name in the enclosing). So I would not expect to be able to refer to existing variables at all in this context. The docs about scope also seems consistent with that.

The fact that `for x = 1:3` introduces a new `x`, while `for x[1] = 1:3` refers to `x` from an outer scope seems pretty odd to me. (The closure example `for g() = 1:3` makes a lot more sense, but is pretty useless.)

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 9, 2022, 7:55pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/37 "2022-03-09T19:55:39Z")

</div>

A lot of this is ambiguities arising from allowing `2f` to mean `2*f`. The spaces required for disambiguation are unnecessary in many other languages.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 9, 2022, 8:03pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/38 "2022-03-09T20:03:33Z")

</div>

> [@Jeff\_Emanuel](#):
>
> The spaces required for disambiguation are unnecessary in many other languages.

From my perspective that explains how folks develop this bad habit—it does not justify it.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 9, 2022, 9:16pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/39 "2022-03-09T21:16:00Z")

</div>

> [@yha](#):
>
> And the part about scope seems to say that the variables to the left of the `=` or `in` are _new_ variables (although technically the paragraph only covers the case where there is no existing variable of the same name in the enclosing). So I would not expect to be able to refer to existing variables at all in this context. The docs about scope also seems consistent with that.

Different people, different interpretations.

> The for loop iterates through these values, assigning each one in turn to the variable i.

The first phrase talks about assigning to `i` even before the text says that `for` specifically can implicitly introduce new variables, consequently it can only be talking about a variable `i` that already exists.

> One rather important distinction between the previous while loop form and the for loop form is the scope during which the variable is visible. If the variable i has not been introduced in another scope, in the for loop form, it is visible only inside of the for loop, and not outside/afterwards.

The rest of the paragraph talks about the _exception_ in behavior of the `for` loop, in the specific case the variable was not introduced before, consequently it leaves implicit the previously stated behavior is expected outside this specific case; otherwise it should mention that, differently from the `while` loop, the variable used to control the loop cannot be declared before it.

But yes, instead of variable, the text should probably say _assignment location_ (or assignable location), which is an expression used in some Julia error messages.

```julia
julia> a = [1]
1-element Array{Int64,1}:
 1

julia> true && a[1] = 2
ERROR: syntax: invalid assignment location "true && a[1]" around REPL[2]:1
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [March 9, 2022, 10:41pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/40 "2022-03-09T22:41:09Z")

</div>

> [@Henrique\_Becker](#):
>
> The rest of the paragraph talks about the _exception_ in behavior of the `for` loop, in the specific case the variable was not introduced before

I can see how this is a possible interpretation, but under that interpretation the docs are just wrong, rather than simply misleading. `for x in ..` introduces a new `x` regardless of whether `x` is already defined:

```julia
julia> x = 2
2

julia> for x = 1:5
       end

julia> x
2

```

[Previous page](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323.md?page=1)

[Next page](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323.md?page=3)
