The ‘where’ keyword

function dimension(::BoseFS{N,M}) where {N,M}
return binomial(BigInt(N + M - 1), BigInt(N))
end

what does the ‘where’ keyword mean? I tend to interprete it like the english clause, but I cannot make sense of it

Agreed. The Julia where keyword seems only about 2/3 aligned with the English word “where” and how it sets up subordinate clauses, and I haven’t yet found documentation that provides an alternate way to understand it.

EDIT: Of course, after saying that I look at the docs and find the answer immediately:


help?> where
search: where

  where

  The where keyword creates a type that is an iterated union of other types, over all values of some variable. For example Vector{T} where
  T<:Real includes all Vectors where the element type is some kind of Real number.

  The variable bound defaults to Any if it is omitted:

  Vector{T} where T    # short for `where T<:Any`

  Variables can also have lower bounds:

  Vector{T} where T>:Int
  Vector{T} where Int<:T<:Real

the key part being "where T # short for where T<:Any

3 Likes

Remember you can use the ? in the repl to bring up help for any keyword.


help?> where
search: where

  where


  The where keyword creates a type that is an iterated union of other types, over all values of some variable. For example
  Vector{T} where T<:Real includes all Vectors where the element type is some kind of Real number.

  The variable bound defaults to Any if it is omitted:

  Vector{T} where T    # short for `where T<:Any`


  Variables can also have lower bounds:

  Vector{T} where T>:Int
  Vector{T} where Int<:T<:Real


  There is also a concise syntax for nested where expressions. For example, this:

  Pair{T, S} where S<:Array{T} where T<:Number


  can be shortened to:

  Pair{T, S} where {T<:Number, S<:Array{T}}


  This form is often found on method signatures.

  Note that in this form, the variables are listed outermost-first. This matches the order in which variables are substituted
  when a type is "applied" to parameter values using the syntax T{p1, p2, ...}.

it’s T where T<:Foo, but you can leave out the <:Foo if Foo is Any.

3 Likes

Yep. :slight_smile: