# ∪ doesn't parse as expected

**URL:** <https://discourse.julialang.org/t/doesnt-parse-as-expected/97671>\
**Category:** General Usage\
**Tags:** parsing\
**Created:** [April 19, 2023, 7:26pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671 "2023-04-19T19:26:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 19, 2023, 7:26pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/1 "2023-04-19T19:26:20Z")

</div>

```julia
julia> 1..2 ∪ 3..5
ERROR: syntax: extra token ".." after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

That’s a bummer. I was hoping to use this syntax inside a macro expansion, but it doesn’t even parse. What are the alternatives? Any good unicode to recommend?

---

<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:** [April 19, 2023, 7:46pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/2 "2023-04-19T19:46:38Z")

</div>

> [@cstjean](#):
>
> `1..2 ∪ 3..5`  
> `ERROR: syntax: extra token ".."`

`∪` isn’t the problem, `..` is.

```julia
julia> (1:2) ∪ (3:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 19, 2023, 7:48pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/3 "2023-04-19T19:48:52Z")

</div>

Right, but in terms of “I want to offer appealing syntax to my macro users”, having to add parentheses is a downer.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [April 19, 2023, 9:20pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/4 "2023-04-19T21:20:49Z")

</div>

> [@Jeff\_Emanuel](#):
>
> `∪` isn’t the problem, `..` is.

I don’t follow you. Both examples work with parentheses and don’t work without them, so in this respect `:` and `..` behave in the exact same way.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 19, 2023, 9:54pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/5 "2023-04-19T21:54:25Z")

</div>

Yes. Both are the same, both need parentheses, both suck 🙂 Is there any operator which looks better and whose precedence makes it work without parens? I looked at the operator list, and ` 1..2 || 3..4` works, but it has other problems.

I think this is probably unsolvable…

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 19, 2023, 10:07pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/6 "2023-04-19T22:07:31Z")

</div>

You want something on this line:

> <https://github.com/JuliaLang/julia/blob/174e138d023cbbcd64fff0e2642583b317770450/src/julia-parser.scm#L18>

```julia
julia> const ⫫ = ∪
union (generic function with 5 methods)

julia> 1:2 ⫫ 3:4
4-element Vector{Int64}:
 1
 2
 3
 4

julia> :(1:2 ⫫ 3:4).args
3-element Vector{Any}:
 :⫫
 :(1:2)
 :(3:4)

```

---

<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:** [April 19, 2023, 11:28pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/7 "2023-04-19T23:28:55Z")

</div>

Is your desired syntax simple enough to edit the text before standard parsing? A macro can take in a String, and such macros have syntactic sugar for [non-standard string literals](https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals). I’m imagining it’d be written like `setop"1..2 ∪ 3..5"` and the String changed to `"(1:2) ∪ (3:5)"` before `parse`ing.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 19, 2023, 11:35pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/8 "2023-04-19T23:35:09Z")

</div>

Having a macro that parses differently from regular Julia code would be confusing, but it does seem like the Julia operator precedence is kinda bad here.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [April 20, 2023, 5:38am UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/9 "2023-04-20T05:38:46Z")

</div>

> [@jar1](#):
>
> Having a macro that parses differently from regular Julia code would be confusing, but it does seem like the Julia operator precedence is kinda bad here.

Only tangentially related since it doesn’t actually solve the original request, but I’ve been wondering what Julia would look like if it had an operator precedence system like [Better operator precedence](https://www.scattered-thoughts.net/writing/better-operator-precedence/).

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 20, 2023, 5:50am UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/10 "2023-04-20T05:50:58Z")

</div>

Yeah, [Operator Precedence: We can do better • The Adamant Programming Language Blog](https://blog.adamant-lang.org/2019/operator-precedence/)

Fortress had it too [JuliaCon 2016 (Keynote) | Fortress Features and Lessons Learned | Guy Steele - YouTube](https://youtu.be/EZD3Scuv02g)

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 20, 2023, 9:47am UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/11 "2023-04-20T09:47:12Z")

</div>

> [@cstjean](#):
>
> Right, but in terms of “I want to offer appealing syntax to my macro users”, having to add parentheses is a downer.

Personally, I like parentheses.

With the exception of the really common operators that have the same precedence in every language, I think that code is more readable with parentheses than without.

(My editor is colouring matching parentheses, which helps a lot for readability. But I also liked them before it had that feature.)

Conversely, I think a language that allowed the user to remove parentheses by re-defining the order of precedence would be very hard to read for me.

---

<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:** [April 20, 2023, 3:09pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/12 "2023-04-20T15:09:41Z")

</div>

It must be that I’m running an older version–I still get an error when adding parentheses in the original:

```julia
julia> (1..2) ∪ (3..5)
ERROR: UndefVarError: .. not defined
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [April 20, 2023, 3:25pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/13 "2023-04-20T15:25:46Z")

</div>

I believe this syntax comes from Intervals.jl (but could be any custom defintion for `..`)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 20, 2023, 3:26pm UTC](https://discourse.julialang.org/t/doesnt-parse-as-expected/97671/14 "2023-04-20T15:26:57Z")

</div>

It’s for a macro, so the important thing is for it to parse as quoted.

```julia
julia> :((1..2) ∪ (3..5))
:((1 .. 2) ∪ (3 .. 5))

```
