# 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:** 3

<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, 10:55pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/41 "2022-03-09T22:55:30Z")

</div>

> for x in … introduces a new x regardless of whether x is already defined:

The text is inaccurate but not entirely false.

```julia
julia> let
       x = 2
       for outer x = 1:5
       end
       @show x
       end
x = 5

```

The keyword `outer` is necessary to use an already defined `x`, however, it cannot be in `global` scope (i.e., the variable must be from `local` scope).

---

<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, 10:57pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/42 "2022-03-09T22:57:22Z")

</div>

> [@jw3126](#):
>
> ```julia
> julia> isa isa where where where
> true
> 
> ```

This is a beautiful abomination 😂

It took me a little while to figure out why this even parses or what it means. Here’s what’s going on:

1. `T where T` is just a confusing way of writing `Any`
2. `where` is a contextual keyword, so the `T` may be replace with `where` to get `where where where`
3. `x isa Any` for all values `x`
4. In particular, `isa isa Any`
5. Putting that together gives us the beautiful `isa isa where where where` as another name for `true`.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [March 11, 2022, 8:36pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/43 "2022-03-11T20:36:28Z")

</div>

> [@c42f](#):
>
> `where` is a contextual keyword

Huh, never would have guessed:

```julia
julia> where = "I can assign a value to `where`."
"I can assign a value to `where`."

```

Looks like all syntax highlighters need to update their rules 😜

---

<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 12, 2022, 12:02am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/44 "2022-03-12T00:02:22Z")

</div>

In general the Julia parser tries not to steal identifiers as keywords so there’s several words (`abstract`, `as`, `doc`, `mutable`, `outer`, `primitive`, `type`, `var`, `isa`, `where`) which are normally identifiers but have special parsing rules in some circumstances.

There’s two circumstance (quoting with `:` and field access with `.`) where words which are normally keywords become identifiers:

```julia
x.end = :for

```

A downside of this is that a syntax highlighter would need to be a full parser to get the highlighting 100% correct.

---

<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:** [March 12, 2022, 3:48pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/45 "2022-03-12T15:48:27Z")

</div>

Unless you overload `getproperty`, there doesn’t appear to be many situations where you can actually get a property named `end`:

```julia
julia> struct A
           end

julia> (a=1, end=2)
ERROR: syntax: unexpected "end"

```

In the first example, the REPL finished the struct definition before I could add another `end`. 😂

VS Code does fine with highlighting symbols:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/10be51063f9de5c175d621f87f137e9ad2b9b1fc.png)

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [March 12, 2022, 4:59pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/46 "2022-03-12T16:59:16Z")

</div>

```julia
julia> struct A
           var"end"
       end

julia> A(1).end
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:** [March 12, 2022, 5:02pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/47 "2022-03-12T17:02:58Z")

</div>

Ha, ok. In that case I guess I’m obligated to report that VS Code does not get the highlighting for `end` correct:

![image](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c1cbd0f05bc605fa7d0e46f2b3aa08df8c0c598.png)

---

<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 14, 2022, 2:20am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/48 "2022-03-14T02:20:50Z")

</div>

> [@CameronBieganek](#):
>
> VS Code does fine with highlighting symbols

Yeah the rules are (mostly?) local enough that syntax highlighting should be able to get them right though it’ll need some lookahead / lookbehind for things like `outer` which are somewhat more annoying. `outer` is a keyword here: `for outer i = 1:10` and here `for outer i (in) 1:10` but not here: `for outer = 1:10` or here `for outer in 1:10`.

---

<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 14, 2022, 2:23am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/49 "2022-03-14T02:23:45Z")

</div>

Speaking of `outer`… here’s a puzzle I’ve concocted using some other tricks up-thread, and another fairly serious WAT I recently discovered.

Does anything get printed here?

```julia
let isa = isa isa where where where
    outer = false
    for outer outer in isa
    end
    try
        if outer isa Any
            @info "Certainly true, right?"
        end
    finally
        outer = false
    catch
        if outer
            @info "Surprise?"
        end
    end
end

```

(Edit: I think it’s fair to say this isn’t exactly a WAT in itself, as it’s intentionally obtuse 😆 )

---

<div class="post-metadata">

**Author:** ![Jerry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry/32/34537_2.png) [@Jerry](https://discourse.julialang.org/u/Jerry)\
**Post date:** [March 14, 2022, 5:42am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/50 "2022-03-14T05:42:47Z")

</div>

Neither corner case nor rare specimen.

```nohighlight
println("What a lovely day!")
for i in 1 : 100_000_000
    for j in 1 : 10_000
        x = cos(j)
    end
end
# Next day...
println("WAT!")); # Syntax error discovered

```

---

<div class="post-metadata">

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

</div>

For me the actual WAT is writing code like this 😀 Still fun to see of course.

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [March 14, 2022, 6:29am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/52 "2022-03-14T06:29:39Z")

</div>

Even more baffling is reading this post, trying to figure out what’s going on and getting this help message.

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.7.2 (2022-02-06)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

help?> outer
search: count_zeros RoundToZero OutOfMemoryError ConcurrencyViolationError countlines count_ones RoundNearestTiesUp RoundNearestTiesAway

Couldn't find outer
Perhaps you meant one, ones, open, operm, pointer, quote, Timer, filter, put!, step, uperm, Number, Ptr or count
  No documentation found.

  Binding outer does not exist.

```

I know it is not a reserved keyword, but still…

---

<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 14, 2022, 7:28am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/53 "2022-03-14T07:28:29Z")

</div>

That’s a good one. I submitted an issue:

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

---

<div class="post-metadata">

**Author:** ![stepanzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stepanzh/32/34851_2.png) [@stepanzh](https://discourse.julialang.org/u/stepanzh)\
**Post date:** [March 29, 2022, 11:35am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/54 "2022-03-29T11:35:36Z")

</div>

Keywords with exclamation mark

```julia
julia> foo!(x; f!) = f!(x)
foo (generic function with 1 method)

julia> bar!(x) = begin x[1] = 0; return x end
bar! (generic function with 1 method)

julia> foo!([1, 2, 3]; f!=bar!)
ERROR: syntax: invalid keyword argument syntax "(f != bar!)" around REPL[18]:1
Stacktrace:
 [1] top-level scope
   @ REPL[18]:1

julia> foo!([1, 2, 3]; f! =bar!)
3-element Vector{Int64}:
 0
 2
 3

```

---

<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 29, 2022, 12:45pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/55 "2022-03-29T12:45:43Z")

</div>

Sorry for being a bit square—but this is really not a bad behavior.

1. this `f!=bar!` example is pretending that spaces dont matter. they absolutely do, and that is well-known
2. _the runtime is throwing an exception to remind you it is wrong to omit spaces_

If we mark this sort of example as a “most harrowing WAT” then what do we call this

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

julia> f(1)
1

```

Is this now an “unmitigated-disaster-level WAT” because there is no exception?

I hope we can agree that my example here is just a plain old bug because of bad code—and so is the `f!=bar!` example.

---

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

</div>

```julia
julia> x = [1, 2];

julia> d = Dict(x => 1)
Dict{Vector{Int64}, Int64} with 1 entry:
  [1, 2] => 1

julia> push!(x, 3);

julia> d
Dict{Vector{Int64}, Int64} with 1 entry:
  [1, 2, 3] => 1

julia> d[[1, 2]]
ERROR: KeyError: key [1, 2] not found

julia> d[[1, 2, 3]]
ERROR: KeyError: key [1, 2, 3] not found

```

Moral: Don’t use mutable keys.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 30, 2022, 5:03am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/57 "2022-03-30T05:03:42Z")

</div>

Somehow, I am glad this doesn’t work!

---

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

</div>

The default behavior of `==` when comparing structs has always been a bit of a WAT to me.

```julia
julia> struct A
           x::Vector{Int}
       end

julia> A([1, 2]) == A([1, 2])
false

julia> mutable struct B
           x::Int
       end

julia> B(1) == B(1)
false

```

In my opinion, the default behavior should be to compare the values of the contents of the structs, regardless of whether the structs or the contents of the structs are mutable.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 30, 2022, 6:38am UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/59 "2022-03-30T06:38:19Z")

</div>

> [@CameronBieganek](#):
>
> In my opinion, the default behavior should be to compare the values of the contents of the structs, regardless of whether the structs or the contents of the structs are mutable.

That’s actually (kind-of) already tracked as a [2.0 issue](https://github.com/JuliaLang/julia/issues/40717), though there’s probably more discussion to be had on how exactly this should work.

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [May 13, 2022, 11:22pm UTC](https://discourse.julialang.org/t/a-most-harrowing-collection-of-julia-wats/77323/60 "2022-05-13T23:22:15Z")

</div>

This is not so much of a WAT for everyone, but… WAT!!!

```julia
julia> macro weird(a::Int)
           @show a, typeof(a)
           1
       end
@weird (macro with 1 methods)

julia> macro weird(a::Float64)
           @show a, typeof(a)
           2a
       end
@weird (macro with 2 methods)

julia> macro weird(a::String)
           @show a, typeof(a)
           a^3
       end
@weird (macro with 3 methods)

julia> macro weird(a::Symbol)
           @show a, typeof(a)
           :($(a)^4)
       end
@weird (macro with 4 methods)

julia> macro weird(a::Expr)
           @show a, typeof(a)
           a
       end
@weird (macro with 5 methods)

julia> @weird 10
(a, typeof(a)) = (10, Int64)
1

julia> @weird 1.0
(a, typeof(a)) = (1.0, Float64)
2.0

julia> @weird "Hi"
(a, typeof(a)) = ("Hi", String)
"HiHiHi"

julia> @weird im
(a, typeof(a)) = (:im, Symbol)
1 + 0im

julia> @weird "Hi" ^ 3
(a, typeof(a)) = (:("Hi" ^ 3), Expr)
"HiHiHi"

```

I would imagine that macro calling logic would consider all arguments as “typeless”, but that doesn’t seem to be the case.

(Without knowing the specifics, I guess that macros are “just” functions underneath that have a special syntax and run on a different moment under the Julia parsing-lowering-compilation-execution pipeline. i.e., just after the parsing and before the lowering).

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

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