# Missed the WAT thread, so here's one for intentionally cursed code

**URL:** https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446
**Category:** Offtopic
**Tags:** joke
**Created:** [May 19, 2024, 4:40am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446 "2024-05-19T04:40:02Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 19, 2024, 4:40am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/1 "2024-05-19T04:40:02Z")

</div>

`continue`s in a 1-iteration loop acting like `@goto`s to 1 `@label` because `break` wasn’t bad enough

```julia
function foo(A)
    for _ in 1
        if is1(A) continue end # @goto wrapup
        bar1!(A)
        if is2(A) continue end # @goto wrapup
        bar2!(A)
    end # @label wrapup
    baz(A)
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: [May 21, 2024, 2:04pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/2 "2024-05-21T14:04:25Z")

</div>

I can’t really compete with this, but I just spent 20 minutes chasing down a bug which was essentially a

```julia
i -= 1

```

misspelled as a

```julia
i =- 1

```

within a branch that was rarely invoked.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 21, 2024, 3:16pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/3 "2024-05-21T15:16:37Z")

</div>

> [@Tamas\_Papp](#):
>
> `i =- 1`

How does Julia parsing work here?  
Extending your example:

```julia
julia> + - + 1
-1

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [May 21, 2024, 3:23pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/4 "2024-05-21T15:23:12Z")

</div>

> [@rafael.guerra](#):
>
> How does Julia parsing work here?

That’s easy to discover.

```julia
julia> Meta.parse("i =- 1")
:(i = -1)

```

---

<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: [May 21, 2024, 5:00pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/5 "2024-05-21T17:00:18Z")

</div>

> [@rafael.guerra](#):
>
> Extending your example:

Unary +/- has [high precedence](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity), so it binds to the argument in a nested way. Specifically,

```julia
julia> Meta.parse("+ - + 1")
:(+(-(+1)))

```

Note that I am not blaming Julia for my `=-` mixup, I think it is perfectly OK to parse that. One cannot expect the parser to catch all mistakes, I should have been writing unit tests for seemingly simple building blocks too.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [May 21, 2024, 5:39pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/6 "2024-05-21T17:39:05Z")

</div>

Just playing around I found out one can do:

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

g(x) = f(x)(x)

g(1) = -1

```

Which is quite weird, but neat to know.

---

<div class="post-metadata">

### Author: ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)
#### Post date: [May 21, 2024, 6:33pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/7 "2024-05-21T18:33:14Z")

</div>

This should arguably be a parser warning

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [May 21, 2024, 10:55pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/8 "2024-05-21T22:55:14Z")

</div>

```julia
julia> struct X
           return 1
       end
1

julia> X()
ERROR: MethodError: no method matching X()

julia> foo(x::T) where {T <: return 1} = nothing
1

julia> foo(1)
ERROR: MethodError: no method matching foo(::Int64)

```

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [May 22, 2024, 1:47am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/9 "2024-05-22T01:47:43Z")

</div>

This is cursed. IDK what’s going on right now. Maybe it’s an undefined behavior or some weird interactions between language semantics.

---

<div class="post-metadata">

### Author: ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)
#### Post date: [May 22, 2024, 2:29am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/10 "2024-05-22T02:29:52Z")

</div>

```julia
julia> where where where where where where where
Any

```

---

<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: [May 22, 2024, 2:50am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/11 "2024-05-22T02:50:02Z")

</div>

you can have much more fun than that.

```julia
julia> why where when where what where who where why
Any

```

---

<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: [May 22, 2024, 3:39am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/12 "2024-05-22T03:39:45Z")

</div>

This is known and intentional, see

> [@Hide default constructor for a struct](https://discourse.julialang.org/t/hide-default-constructor-for-a-struct/89064/11):
>
> Not a bug. The struct has local scope and you can evaluate arbitrary code inside to define ways to construct objects of the type. The most common way to do this is to add methods to the type object itself, but you can do other things as well, eg define a single globally bound instance or a global function by another name that constructs them. If there is any code in a strict you don’t get a default constructor.

but I am not sure it is mentioned in the manual.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 22, 2024, 9:04am UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/13 "2024-05-22T09:04:46Z")

</div>

> [@adienes](#):
>
> `foo(x::T) where {T <: return 1} = nothing`

To clarify, this occurs without the `struct X return 1 end` too, and it doesn’t make a method according to `methods(foo)`. As far as I can tell from `Meta.@lower`, it’s the same behavior as early returns from a much less cursed `let` block, and early returns are treated the same as any other code in local scopes.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [May 22, 2024, 1:43pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/14 "2024-05-22T13:43:51Z")

</div>

Please explain this one?

---

<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: [May 22, 2024, 1:48pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/15 "2024-05-22T13:48:00Z")

</div>

This basically reduces to

```julia
julia> A where B where A
Any

```

which I don’t fully understand why it doesn’t complain about `B` not being defined, but from there it’s just tacking on extra clauses.

---

<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: [May 22, 2024, 1:56pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/16 "2024-05-22T13:56:36Z")

</div>

> [@Oscar\_Smith](#):
>
> I don’t fully understand why it doesn’t complain about `B` not being defined

The right hand side of the `where` introduces a new binding. You don’t necessarily need to use it:

```julia
julia> Int where asdf
Int64

```

This is introducing lots of unused where bindings. The only thing that needs to be defined — either by an existing binding or one of the newly introduced ones with the `where`s — is the leftmost one.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 22, 2024, 3:06pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/17 "2024-05-22T15:06:00Z")

</div>

Some more REPL clarification:

```julia
julia> A where A
Any

julia> Meta.@lower A where A
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Core.TypeVar(:A)
│ A = %1
│ %3 = Core.UnionAll(A, A)
└── return %3
))))

```

I’m pretty sure that’s a nothingburger because involving curly braces makes `apply_type` throw an error if a parameter tries to be the parametric type:

```julia
julia> A{Int} where A
ERROR: TypeError: in Type{...} expression, expected UnionAll, got a value of type TypeVar
Stacktrace:
 [1] top-level scope
   @ REPL[63]:1

julia> Meta.@lower A{Int} where A
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Core.TypeVar(:A)
│ A = %1
│ %3 = A
│ %4 = Core.apply_type(A, Int)
│ %5 = Core.UnionAll(%3, %4)
└── return %5
))))

```

As for the sequence of `where`s, some of them are not the keyword `where`, just variables:

```julia
julia> where where where
Any

julia> Meta.@lower where where where
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Core.TypeVar(:where)
│ where = %1
│ %3 = Core.UnionAll(where, where)
└── return %3
))))

```

---

<div class="post-metadata">

### Author: ![maxfreu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxfreu/32/17468_2.png) [@maxfreu](https://discourse.julialang.org/u/maxfreu)
#### Post date: [May 22, 2024, 3:16pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/18 "2024-05-22T15:16:03Z")

</div>

Cursed code straight [from the manual](https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling):

```julia
sqrt_second(x) = try
    sqrt(x[2])
catch y
    if isa(y, DomainError)
        sqrt(complex(x[2], 0))
    elseif isa(y, BoundsError)
        sqrt(x)
    end
end

sqrt_second([1,"2",3]) # no problemo!

```

The way error handling works in julia and that you have to explicitly re-throw errors makes me nervous and angry every time I have to do it.

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [May 22, 2024, 3:37pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/19 "2024-05-22T15:37:27Z")

</div>

I’m not attempting to criticize your post just understand it — isn’t that the whole point of a `catch` block? why would it rethrow automatically

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [May 22, 2024, 5:00pm UTC](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446/20 "2024-05-22T17:00:29Z")

</div>

Python has switch-like error handling where you can do

```python
try:
    # some error code
except ExpectedErrorType:
    # handle that exception
except AnotherErrorType:
    # another way to handle error

```

If the error is not one of `ExpectedErrorType` or `AnotherErrorType`, Python falls back to rethrowing the error. It is considered bad practice in Python to do `except:` and generically handle all exceptions.

Coming from Python, I had similar friction with Julia’s try-catch syntax.

[Next page](https://discourse.julialang.org/t/missed-the-wat-thread-so-heres-one-for-intentionally-cursed-code/114446.md?page=2)
