"where" - where?


#1

The following quote is an excerpt from the latest version of the manual:

(Equivalently, one could define function norm{T<:Real}(p::Point{T}) or function norm(p::Point{T} where T<:Real); see UnionAll Types.)

Notice how the where clause lies inside the parentheses specifying the function arguments in this example. Elsewhere in the manual, the where clause always seems to be after the parentheses. Is this a typo in the manual, or is the where-clause allowed inside? Is there a distinction in meaning for inside versus outside? I tried to put the where-clause inside the parentheses (next to the parameterized argument) in my own code. I didn’t get a syntax error, but I did get an error at run-time about an undefined type.


#2

It is allowed inside.
If inside, you wont have access to the parameter inside the function. You do if outside.
Compare

function f(x::Vector{T} where T <: Real)
  out = zero(T)
  for x_i ∈ x
    out += x_i
  end
  out
end
function g(x::Vector{T} where T <: Real)
  out = zero(eltype(x))
  for x_i ∈ x
    out += x_i
  end
  out
end

function h(x::Vector{T}) where {T <: Real}
  out = zero(T)
  for x_i ∈ x
    out += x_i
  end
  out
end

Running these:

julia> x = randn(20);

julia> sum(x)
-0.8093970114745281

julia> f(x)
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] f(::Array{Float64,1}) at ./REPL[16]:2

julia> g(x)
-0.8093970114745275

julia> h(x)
-0.8093970114745275