# Anybody else find the new where syntax less than satisfactory?

**URL:** https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897
**Category:** General Usage
**Created:** [July 17, 2017, 9:39am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897 "2017-07-17T09:39:25Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [July 17, 2017, 9:39am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/1 "2017-07-17T09:39:25Z")

</div>

things like

same\_type(x::T, y::T) where {T} = true  
myappend(v::Vector{T}, x::T) where {T} = [v…, x]  
mytypeof(x::T) where {T} = T

I find utterly confusing…  
It’s only really readable when there is an actual constraint on the type param like  
same\_type(x::T, y::T) where {T \<: Number } = true

Not to mention it breaks a lot of existing code…

Personally I prefer the old syntax by a lot

I’m not exactly sure why the change was necessary but if it had to do with confusion between method definition and calling parametric types as functions, then why not simply introduce a ‘method’ keyword to disambiguate?

so: method same\_type{T}(x::T, y::T) = true

---

<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: [July 17, 2017, 9:46am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/2 "2017-07-17T09:46:37Z")

</div>

A lot of thought went into these changes. If you are interested in the discussion, start with  
[https://github.com/JuliaLang/julia/issues/8974](https://github.com/JuliaLang/julia/issues/8974)  
[https://github.com/JuliaLang/julia/pull/18457](https://github.com/JuliaLang/julia/pull/18457)  
[https://github.com/JuliaLang/julia/pull/20414](https://github.com/JuliaLang/julia/pull/20414)  
[https://github.com/JuliaLang/julia/issues/11310](https://github.com/JuliaLang/julia/issues/11310)  
[https://github.com/JuliaLang/julia/pull/20308](https://github.com/JuliaLang/julia/pull/20308)

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [July 17, 2017, 9:53am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/3 "2017-07-17T09:53:36Z")

</div>

I definitely disagree. When you are overloading the same function with many different and long templated variables, the function signature becomes unreadable.

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [July 17, 2017, 11:16am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/4 "2017-07-17T11:16:11Z")

</div>

> same\_type(x::T, y::T) where {T} = true

For simple cases like this old syntax is still supported (at least on 0.6):

```julia
julia> same_type{T}(x::T, y::T) = true
same_type (generic function with 1 method)

```

---

<div class="post-metadata">

### Author: ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)
#### Post date: [July 17, 2017, 1:46pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/5 "2017-07-17T13:46:58Z")

</div>

While I prefer the new syntax, I must admit, I still have a mental hiccup when reading the “where” term. I always translate it to something like “for\_a\_certain”, “with\_type”, or “for\_all”.

Where I have questions though are the curly brackets. It seems confusing, sometimes there is a `where {T}` but sometimes a pure `where T`.

Tendencially in one-liner functions `{T}` is being used and in full (multiline) functions `T`. But `ack "where {T" base/` and `ack "where T" base/` show that this is not (yet?) the case everywhere, e.g.

```julia
HistoryPrompt(hp::T) where T<:HistoryProvider = HistoryPrompt{T}(hp)

function TwicePrecision{T}(nd::Tuple{I,I}) where {T,I}
    n, d = nd
    TwicePrecision{T}(n, zero(T)) / d
end

```

Maybe I made this up and there isn’t such a convention, not sure. It was discussed in the above-mentioned [20308](https://github.com/JuliaLang/julia/pull/20308) issue. Personally I’d have preferred a pure `T` without brackets (and get used to the slightly/much confusing `where T = xy` consequence).

---

<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: [July 17, 2017, 2:42pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/6 "2017-07-17T14:42:19Z")

</div>

I like to write things like

```julia
same_type(x::T, y::T) where {T<:Any} = true

```

The pleonastic `<:Any` makes it much easier for me to mentally parse the code, and also clarifies that I really want this to apply to any type (as opposed to “I was too lazy to specify”).

---

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [July 17, 2017, 2:53pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/7 "2017-07-17T14:53:06Z")

</div>

that’s nice but there will still be a whole bunch of code written by others that won’t follow this style and so you’ll still have to know the other form too (That 's basically the problem with C++ code: you can write nice code if you adhere to a subset of C++ but everybody uses a different subset 😉 )

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [July 17, 2017, 3:17pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/8 "2017-07-17T15:17:42Z")

</div>

> [@dfdx](#):
>
> For simple cases like this old syntax is still supported (at least on 0.6):

But will probably be deprecated soon:

> <https://github.com/JuliaLang/julia/pull/22834>
>
> OK folks, it's time to rip the band-aid off.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [July 17, 2017, 4:34pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/9 "2017-07-17T16:34:49Z")

</div>

> [@giordano](#):
>
> But will probably be deprecated soon:

Honestly, I’m not going to be too happy when this happens. The new syntax is fine, but the old one is just so much more compact, especially for writing one-line functions (I’m a big stickler about not allowing lines to go past my screen-split).

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [July 17, 2017, 6:12pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/10 "2017-07-17T18:12:45Z")

</div>

I love where. I love words. Programmers should use them more often.

---

<div class="post-metadata">

### Author: ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)
#### Post date: [July 17, 2017, 8:10pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/11 "2017-07-17T20:10:48Z")

</div>

Maybe ∀ can alias for `where`:

`f(x::T, y::T) ∀ T = x+y`

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [July 17, 2017, 10:41pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/12 "2017-07-17T22:41:05Z")

</div>

I am confused about the proper style concerning where the `where` goes. Consider:

```julia
julia> function minplus1(x::Vector{T} where {T <: Real})
           return minimum(x) + 1
       end
minplus1 (generic function with 1 method)

julia> minplus1([1,2,3])
2

julia> function minplus1(x::Vector{T}) where {T <: Real}
           return minimum(x) + 1
       end
minplus1 (generic function with 1 method)

julia> minplus1([4,5,6])
5

```

Does it matter? Do these always perform the same? How about with anonymous functions? The example given in the release-time `NEWS.md` seems to have an awful lot of punctuation: `((x::Array{T}) where T<:Real) -> 2x`.

When Julia `0.5` was released there was a great blog post (by Stefan, I think?) illustrating the key changes from a user perspective. Has there been something similar for `0.6`? I would certainly find that helpful.

---

<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: [July 17, 2017, 10:53pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/13 "2017-07-17T22:53:24Z")

</div>

In the second definition, the binding `T` spans all arguments and is available in the method body.

In the first definition, the binding `T` is only available for that one argument.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 18, 2017, 1:49am UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/14 "2017-07-18T01:49:04Z")

</div>

I explained the overall motivation for the `where` syntax in this StackOverflow answer:

> <https://stackoverflow.com/questions/41931289/where-in-function-definitions-in-julia-0-6/41940710#41940710>

Partly quoting from there (with some clarifications and edits):

Fundamentally, the problem with `F{T}(args...)` in Julia 0.5 and earlier is that the `F{T}` part is ambiguous – the parser knows what it means from the broader context, but its meaning depends heavily on that context. The fact that it can mean such different things is pretty confusing:

- In most contexts, `F{T}` means the parametric type `F` with type parameter `T`.
- Followed by parens in most contexts, `F{T}(args...)` means to apply the type `F{T}` to the arguments `args...` as a function, typically constructing an instance of the type `F{T}`.
- Followed by parens as the left hand side of a method definition, however, as in `F{T}(args...) = expr`, it means to define a method for `F` as a function, with type parameters `T`, formal arguments `args...`, and definition `expr`.

This motley collection of different meanings is not ideal. Indeed, constructor syntax, where these syntaxes collide, was, in my opinion, prior to 0.6, the worst, most confusing part of Julia. Moreover, there were no syntaxes for either of these meanings, both of which can be useful:

- Adding a method to `F{T}` for the concrete value of `T` in the current scope.
- Adding a method to `F{T}` for each parametric value `T`.

The former was previously only possible by assigning a name to `F{T}` and then adding a method to that. The latter was only possible inside of the parametric type block, where `F` by itself was (and still is when you use the deprecated syntax) magically bound to each specific `F{T}` when used as the function name in a method definition. In Julia 1.0, on the other hand, type parameters and constructors will be thoroughly consistent, following these general principles uniformly:

- The syntax used to define a method _always_ matches the syntax used to call it.
- The `F{T}` syntax _always_ refers to the type `F` with parameter value `T`.
- Type parameters are _always_ introduced by `where` clauses.

I think these principles make the syntax and semantics of parametric types and methods far more understandable and intuitive. Given these principles, semantics like what we now have seem inevitable. The only real question is syntax – and what syntax to use was one of the biggest bikesheds around this issue. I wasn’t initially thrilled with the postfix `where` syntax, but it’s grown on me and now it seems quite natural. The only really odd case, as you mention, is `f(...) where T = body` without any type bound, but I’ve found that even this case fairly quickly loses its unfamiliarity. Other keywords than `where` were discussed, including `forall` and `∀`. However, Julia’s “union all” types are _not_ universally quantified types (they’re actually closer to existentially quantified types), so both of these choices would have been actively at odds with existing type theory nomenclature. The `where` keyword was the most evocative choice proposed that didn’t clash with well-established terminology. Finally, having the `where` clause on the right just seemed to read much more naturally in the vast majority of usages.

---

<div class="post-metadata">

### Author: ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)
#### Post date: [December 15, 2019, 8:59pm UTC](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/15 "2019-12-15T20:59:19Z")

</div>

It would be great if Julia documentation further explained how to use **Where** and provided more examples.
