# Changing precedence of the piping operator

**URL:** <https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [June 4, 2020, 1:49pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730 "2020-06-04T13:49:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 4, 2020, 1:49pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730/1 "2020-06-04T13:49:12Z")

</div>

In most uses of functional-style piping I’ve seen in Julia, it seems that a lot of people don’t realize that `|>` has precedence over `->`. So

```julia
data |>
x -> f(x) |>
g

```

parses to

```julia
data |>
x -> g(f(x))

```

rather than

```julia
data |>
(x -> f(x)) |>
g

```

In most cases, it doesn’t really matter because it works out the same either way, but it can do some unexpected things with broadcasted piping:

```julia
julia> (1, 2) .|>
       x -> x^2 |>
       sum
(1, 4)

julia> (1, 2) .|>
       (x -> x^2) |>
       sum
5

```

I bring this up because I see this mistake _everywhere_. I’ve seen it on questions posted here. I’ve seen it in blog posts from pretty experienced data scientists. I even saw it in a tutorial this morning.

This seems like one of those “if everyone is walking over the same path on a lawn, maybe we should pave a walkway over that path” situations. I can only see this type of usage increasing as functional programming styles are becoming more popular. Of course, it could be mentioned in up front the docs that it is a good practice to use parentheses in this case to avoid ambiguity, but [there are only so many things we can put up front in the docs](https://discourse.julialang.org/t/the-speed-of-light-is-an-integer-why-should-we-care/40108).

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 4, 2020, 2:28pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730/2 "2020-06-04T14:28:33Z")

</div>

If you do `?|>` the example is potentially a source of problem:

```julia
help?> |>
search: |>

  |>(x, f)

  Applies a function to the preceding argument. This allows for easy function
  chaining.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> [1:5;] |> x->x.^2 |> sum |> inv
  0.01818181818181818

```

Maybe just adding parentheses there would help… adding another example with broadcasted piping along the lines that you suggested could be nice too. Like

```julia
julia> (1, 2) .|> (x -> x^2) |> sum
5

```

That could be a nice little PR?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 4, 2020, 2:44pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730/3 "2020-06-04T14:44:00Z")

</div>

Oh, that’s interesting. I hadn’t noticed it there. That PR would certainly help, but I also think this is the type of thing that people tend to use without looking at the help docstring because it seems so self-explanatory.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 4, 2020, 2:50pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730/4 "2020-06-04T14:50:33Z")

</div>

but wouldn’t you say that people who’ve never seen that operator before might try to do `?|>` it seems the easiest way to find info (e.g. you might not even know what to look for, that it’s called “pipe” or whatever).

Anyway I just think if you opened a PR, it would be very welcome; changing precedence would be for Julia 2.0 anyway (and I’m also not convinced it’s a good idea but I’m not very opinionated on the matter).

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 4, 2020, 3:00pm UTC](https://discourse.julialang.org/t/changing-precedence-of-the-piping-operator/40730/5 "2020-06-04T15:00:44Z")

</div>

That’s a good point. Either way, it’s probably the right thing to do, so I’ll open one up later today.

As far as the precedence goes, I would be curious to see cases where changing it breaks code that shouldn’t already be erroring. Or in other words, the code that would break from this change might have been computing something unexpected before since I doubt many people are using something like `(1, 2) .|> x -> x^2 |> sum` to purposely mean `(1, 2) .|> x -> (x^2 |> sum)`.
