Anybody else find the new where syntax less than satisfactory?


#1

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


#2

A lot of thought went into these changes. If you are interested in the discussion, start with






#3

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


#4

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> same_type{T}(x::T, y::T) = true
same_type (generic function with 1 method)

#5

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.

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 issue. Personally I’d have preferred a pure T without brackets (and get used to the slightly/much confusing where T = xy consequence).


#6

I like to write things like

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”).


#7

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 :wink: )


#8

But will probably be deprecated soon:


#9

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).


#10

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


#11

Maybe ∀ can alias for where:

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


#12

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

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.


#13

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.


#14

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

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.


"where" syntax
PSA: Parametric method syntax deprecation about to drop
Help with where syntax in Julia v0.6
Ways of using constructor of a parametric type