# Which of \`::\` and \`where\` should take precedence?

**URL:** <https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807>\
**Category:** Internals & Design\
**Tags:** parametric-methods\
**Created:** [July 26, 2022, 10:36am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807 "2022-07-26T10:36:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaowei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaowei/32/38280_2.png) [@shaowei](https://discourse.julialang.org/u/shaowei)\
**Post date:** [July 26, 2022, 10:36am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807/1 "2022-07-26T10:36:21Z")

</div>

Currently (Julia v1.7), `where` takes precedence over `::`. For example:

```julia
julia> :(f(x::T where T <: Integer))
:(f(x::(T where T <: Integer)))

```

However, this means that the return type annotation will not play nice with parametric methods:

```julia
julia> f(x::T)::Int where T<:Integer = x
ERROR: UndefVarError: T not defined

```

because the parametric definition got bundled with `Int` instead:

```julia
julia> :(f(x::T)::Int where T<:Integer)
:(f(x::T)::(Int where T <: Integer))

```

The workaround is to do either of:

```julia
julia> (f(x::T)::Int) where T<:Integer = x

julia> function (f(x::T)::Int) where T<:Integer
           return x
       end

```

which looks like code-smell…

As such, I would like to discuss the possibility of having `::` take precedence over `where` instead. This would give us the following result:

```julia
julia> :(f(x::T where T <: Integer))
:(f((x::T) where T <: Integer))

julia> :(f(x::T)::Int where T<:Integer)
:((f(x::T)::Int) where T <: Integer)

```

and so the outer parametric definition correctly tag the function instead of the return type. This way we can go with the cleaner syntax of:

```julia
julia> f(x::T)::Int where T<:Integer = x

julia> function f(x::T)::Int where T<:Integer
           return x
       end

```

or if we so willed it:

```julia
julia> function rand_integer_type()::(T where T<:Integer)
           ...
       end

```

Not sure whether there’s any issue that arises due to this change, but since `f(x::T) where T<:Integer = x` works and:

```julia
julia> :(f(x::T) where T<:Integer)
:(f(x::T) where T <: Integer)

```

I would believe that the parametric information can reach `f`.

Would like to seek our opinion on this please 🙏

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 26, 2022, 10:58am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807/2 "2022-07-26T10:58:26Z")

</div>

See [JuliaLang/julia#21847](https://github.com/JuliaLang/julia/issues/21847).

---

<div class="post-metadata">

**Author:** ![shaowei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaowei/32/38280_2.png) [@shaowei](https://discourse.julialang.org/u/shaowei)\
**Post date:** [July 26, 2022, 11:08am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807/3 "2022-07-26T11:08:42Z")

</div>

Wow how did you reach the exact issue so fast \>\_\< I hope I have better skill when it comes to searching the various Julia resources…

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 26, 2022, 11:17am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807/4 "2022-07-26T11:17:30Z")

</div>

> [@shaowei](#):
>
> This way we can go with the cleaner syntax of:
> 
> ```julia
> julia> f(x::T)::Int where T<:Integer = x
> 
> julia> function f(x::T)::Int where T<:Integer
> return x
> end
> 
> ```

Notice that the second example already works as is. It’s only in “short form” definitions of functions where the issue arises.

---

<div class="post-metadata">

**Author:** ![shaowei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaowei/32/38280_2.png) [@shaowei](https://discourse.julialang.org/u/shaowei)\
**Post date:** [July 27, 2022, 5:11am UTC](https://discourse.julialang.org/t/which-of-and-where-should-take-precedence/84807/5 "2022-07-27T05:11:39Z")

</div>

Ah that’s true, thanks for pointing out. Indeed:

```julia
julia> :(function f(x::T)::Int where T<:Integer
                  return x
              end)

:(function (f(x::T)::Int) where T <: Integer
      #= REPL[1]:1 =#
      #= REPL[1]:2 =#
      return x
  end)

```

That looks like a hack to me though, i.e. I can’t determine the precedent just by looking at the local code of `A::B where C`…
