# semantics of \`where {}\` in parametric methods

**URL:** https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032
**Category:** New to Julia
**Tags:** question
**Created:** [May 21, 2018, 3:04am UTC](https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032 "2018-05-21T03:04:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tomf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomf/32/6995_2.png) [@tomf](https://discourse.julialang.org/u/tomf)
#### Post date: [May 21, 2018, 3:04am UTC](https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032/1 "2018-05-21T03:04:15Z")

</div>

I’m learning Julia from various sources. I’m also reading code to get an idea of how a good Julia codebase is structured (suggestions welcome, BTW). I’ve come across constructs like this:

`TimeNode{V,T}(g::AbstractGraph, key::V, timestamp::T) where {V} where {T} = TimeNode(num_active_nodes(g) + 1, key, timestamp)`

Obviously this is a parametric method definition, but I’ve had difficulty finding a clear rigorous explanation of the **where** keyword—or, for that matter, the syntax and semantics of such definitions. The closest is in the _Parametric Methods_ section of the Julia reference manual, but that gives examples without a careful explanation.

I understand that `where {V<:X)` constrains V to be a subtype of X, but not what `where {V}` alone does. Does order matter? Does order matter in the `{V,T}` clause? How do the semantics change if one of these is missing? etc.

Thanks,  
-Tom

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [May 21, 2018, 4:41am UTC](https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032/2 "2018-05-21T04:41:47Z")

</div>

The most recent version of the documentation (‘latest’) has a much better description of parametric methods: [https://docs.julialang.org/en/latest/manual/methods.html#Methods-1](https://docs.julialang.org/en/latest/manual/methods.html#Methods-1)

Best I can tell, `where {V}` binds the concrete type of the function’s argument to a variable `V` visible within the function’s scope.

```julia
function mytypeof(x::T) where {T}
    return T
end

```

```julia
julia> mytypeof(2.0)
Float64

```

Additionally, `where` constrains the type signature of all corresponding arguments to the same concrete type, while annotating individual arguments does not.

```julia
f1(x::Number, y::Number) = x + y
f2(x::T, y::T) where {T<:Number} = x + y

```

```julia
julia> f1(2, 2.0)
4.0

julia> f2(2, 2.0)
ERROR: MethodError: no method matching f2(::Int64, ::Float64)
Closest candidates are:
  f2(::T<:Number, ::T<:Number) where T<:Number at REPL[11]:1

```

---

<div class="post-metadata">

### Author: ![tomf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomf/32/6995_2.png) [@tomf](https://discourse.julialang.org/u/tomf)
#### Post date: [May 21, 2018, 12:06pm UTC](https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032/3 "2018-05-21T12:06:36Z")

</div>

Thanks. A few more questions. What’s the difference between:

`mytypeof(x::T) where {T} = T`  
and  
`mytypeof{T}(x::T) = T`

From examples I’m guessing that the two are equivalent. I’m also guessing that the order of the **where** clauses doesn’t matter when you have multiple type parameters.

Thanks,  
-Tom

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 21, 2018, 12:16pm UTC](https://discourse.julialang.org/t/semantics-of-where-in-parametric-methods/11032/4 "2018-05-21T12:16:02Z")

</div>

> [@tomf](#):
>
> What’s the difference between:
> 
> `mytypeof(x::T) where {T} = T`  
> and  
> `mytypeof{T}(x::T) = T`

The latter is deprecated for the former.

> [@tomf](#):
>
> I’m also guessing that the order of the **where** clauses doesn’t matter when you have multiple type parameters.

It matters if you are doing “triangular dispatch” which is now supported. See some discussion at [triangular dispatch (left-to-right template parameter chaining) · Issue #3766 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/3766).
