# \`Where\` questions

**URL:** <https://discourse.julialang.org/t/where-questions/1797>\
**Category:** General Usage\
**Created:** [January 31, 2017, 3:26am UTC](https://discourse.julialang.org/t/where-questions/1797 "2017-01-31T03:26:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 31, 2017, 3:26am UTC](https://discourse.julialang.org/t/where-questions/1797/1 "2017-01-31T03:26:34Z")

</div>

# `Where` questions

Hi! I spent some time today reading the upcoming [0.6 documentation](http://docs.julialang.org/en/latest/manual/types.html#UnionAll-Types-1) and had a few questions while trying to understand the upcoming new syntax for parametric types.

1. What is the difference, if any, between `typealias Vector{T} Array{T,1}` and `const Vector = Array{T, 1} where T`?

2. Of the following two forms, why can you say b). but not a).?

3. What is the difference, if any, between `f{T}(x::T) = x` and `f(x::T) where T = x`?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 31, 2017, 3:44am UTC](https://discourse.julialang.org/t/where-questions/1797/2 "2017-01-31T03:44:22Z")

</div>

> [@yurivish](#):
>
> What is the difference, if any, between typealias Vector{T} Array{T,1} and const Vector = Array{T, 1} where T?

None.

> [@yurivish](#):
>
> a). Tuple{X, Y} where X \<: Real where Y \<: Vector{X}  
> b). Tuple{X, Y} where Y \<: Vector{X} where X \<: Real

`A where B where C` is actually `(A where B) where C`

In another word `B` can use typevar in `C` (or in general any typevar defined later in syntactic order) but not the other way around. If `B` and `C` do not reference each other, the order can be reversed but it’s not possible in general when they are referencing each other as in this case.

> [@yurivish](#):
>
> What is the difference, if any, between f{T}(x::T) = x and f(x::T) where T = x?

None. Except that the former might be getting deprecated due to conflict with a better constructor definition syntax.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 31, 2017, 3:45am UTC](https://discourse.julialang.org/t/where-questions/1797/3 "2017-01-31T03:45:14Z")

</div>

> [@yuyichao](#):
>
> typealias Vector{T} Array{T,1}

And this might also be deprecated as some point since there isn’t really a need for it anymore.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 31, 2017, 4:09pm UTC](https://discourse.julialang.org/t/where-questions/1797/4 "2017-01-31T16:09:45Z")

</div>

So, apparently, reading `where` as **wherein** provides the correct sense of oriented inclusion.

---

<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:** [January 31, 2017, 6:14pm UTC](https://discourse.julialang.org/t/where-questions/1797/5 "2017-01-31T18:14:21Z")

</div>

You gotta remember the implicit `<: Any` or else it makes no sense.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 1, 2017, 9:39am UTC](https://discourse.julialang.org/t/where-questions/1797/6 "2017-02-01T09:39:25Z")

</div>

Related question to the last.

Is `Array{Int, N} where N` really exactly the same as `Array{Int, N} where N <: Any`?

In the latter, `N = 1`, `2`, etc does not satisfy the predicate `N <: Any`, so it seems that should be forbidden…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 1, 2017, 1:29pm UTC](https://discourse.julialang.org/t/where-questions/1797/7 "2017-02-01T13:29:46Z")

</div>

It’s an unrelated issue. [https://github.com/JuliaLang/julia/issues/9580](https://github.com/JuliaLang/julia/issues/9580)

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 1, 2017, 2:04pm UTC](https://discourse.julialang.org/t/where-questions/1797/8 "2017-02-01T14:04:11Z")

</div>

i find this syntax mildly confusing, because it looks like the = is part of the type constraint

```julia
f(x::T) where T = x
t(x::T) where T = T

```

wouldn’t it make more sense to put it inside the parens?

```julia
t(x::T where T) = T

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 1, 2017, 2:11pm UTC](https://discourse.julialang.org/t/where-questions/1797/9 "2017-02-01T14:11:24Z")

</div>

See the on going discussion at [https://github.com/JuliaLang/julia/pull/20308#issuecomment-276229351](https://github.com/JuliaLang/julia/pull/20308#issuecomment-276229351) for a better syntax. The comment linked also shows why putting it inside the parenthesis is wrong.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 1, 2017, 2:37pm UTC](https://discourse.julialang.org/t/where-questions/1797/10 "2017-02-01T14:37:45Z")

</div>

anyone cares to explain what is the problem with this?

`f(t::T where T)::T = ...`

yes T is used after its definition, OK. but the return type is not part of the dispatch mechanism, so how is that different than, say,

`f(t::T) where T = T`

in which case we use T after its definition? what do we lose here?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [February 1, 2017, 3:23pm UTC](https://discourse.julialang.org/t/where-questions/1797/11 "2017-02-01T15:23:28Z")

</div>

Since most of us expect `where` to introduce a clause (i.e. a nontrivial expression) perhaps a better keyword would be something like `for_any`. These seem less confusing than the `where` versions, at least to me:

```julia
f(x::T) for_any T = x
t(x::T) for_any T = T
foo{T}(x) for_any T = rhs

```

And this is about the same (since type assertions are already peculiar):

```julia
foo{T}(x) for_any T<:Real = rhs

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 1, 2017, 3:54pm UTC](https://discourse.julialang.org/t/where-questions/1797/12 "2017-02-01T15:54:02Z")

</div>

> [@pint](#):
>
> anyone cares to explain what is the problem with this?
> 
> f(t::T where T)::T = …

Because it’s `f(t::(T where T))::T = ...` which is `f(t::Any)::T = ...`. The type parameter for the method has to be on the method, it is not if you put it into the argument. The thing before `where` is the thing that’s parametrized. For `f(::T where T)` it’s `T` and for `f(::T) where T` it’s `f(::T)`. You want the latter for parametric method definition.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 1, 2017, 5:29pm UTC](https://discourse.julialang.org/t/where-questions/1797/13 "2017-02-01T17:29:25Z")

</div>

unfortunately juliabox keeps dying for me now, so can’t try. but if i understand correctly, you can do this in 0.6?

`f(x::Pair{T,T} where T, a::Array{T,1} where T) = x, a`

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 1, 2017, 6:09pm UTC](https://discourse.julialang.org/t/where-questions/1797/14 "2017-02-01T18:09:50Z")

</div>

Yes, it constrains the first argument to be a pair with the same type, and a vector that has another type.  
In that form, the Ts in each argument are independent.  
`(g(x::Pair{T,T}, a::Array{T,1}) where T) = x, a` is what you need to constrain the elements of the Pair and of the Vector to all be the same type.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 1:25am UTC](https://discourse.julialang.org/t/where-questions/1797/15 "2017-02-02T01:25:43Z")

</div>

> anyone cares to explain what is the problem with this?
> 
> ```
> f(t::T where T)::T = ...
> 
> ```

The parameter names of `UnionAll` types do not escape the signature. They do not automatically come into scope; they are inside the type. In your first example `T` is just a dummy variable used to discuss the type `(T where T)` which (I think) is converted to `Any`.

When the `where` is outside the signature, that indicates that the _method_ is to be parameterized and the names of the type variables are visible both inside the signature and inside the method body (the `...`).

The thing which “might” make sense is

```julia
(f(t::T) where T)::T = ...

```

which is quite analogous to the allowed

```julia
(f(t::T) where T) = ...

```

given that (as I understand) the method output restriction `::T` piece is transformed to be a part of the `...` anyway.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 2, 2017, 8:29am UTC](https://discourse.julialang.org/t/where-questions/1797/16 "2017-02-02T08:29:30Z")

</div>

juliabox 0.6.0 seems to be permadead to me. is this also possible now?

`isgoodtype(t) = t <: (Pair{T, T} where T)`

that is, this expression is just a regular expression that can be used outside of definitions? because afaik it is not possible with 0.5.0 syntax.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 2, 2017, 1:31pm UTC](https://discourse.julialang.org/t/where-questions/1797/17 "2017-02-02T13:31:19Z")

</div>

Yes. \<20 chars limit\>

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [February 3, 2017, 4:29pm UTC](https://discourse.julialang.org/t/where-questions/1797/18 "2017-02-03T16:29:12Z")

</div>

+1. the juliabox 0.6-dev kernel is always crashing those days for me too
