# Is \`where T\` the same as \`where T \<: Any\` in all cases?

**URL:** <https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329>\
**Category:** General Usage\
**Created:** [February 26, 2018, 3:22am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329 "2018-02-26T03:22:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [February 26, 2018, 3:22am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/1 "2018-02-26T03:22:53Z")

</div>

So, my question is pretty straight-forward (and right there entirely in the title), but to give a bit of background/justification…

I realize that the `function foo(bar::Baz{T}) where T` syntax is relatively new, and so some of my discomfort with it could be chalked-up to unfamiliarity, but even after some months I still think that `function foo(bar::Baz{T}) where T <: Any` reads better. Before I start trying out that style in my own code, I thought I’d just toss this simple question out there: are those equivalent in all cases? I know that conceptually they should be the same, and they are in every case I could think to test, but maybe I’m missing something?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 26, 2018, 3:44am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/2 "2018-02-26T03:44:42Z")

</div>

It’s odd. I would have assumed the answer is “no”, because I can have a type parameter which is not a type (for example, in `Array{Int, 1}`, `1` is an Int, not a type, and `1 <: Any` is not true (in fact it’s nonsensical). However, Julia does consider it a match for `Array{Int, N} where {N <: Any}` as well as `Array{Int, N} where N`:

```julia
julia> Array{Int, 1} <: (Array{Int, N} where {N <: Any})
true

julia> Array{Int, 1} <: (Array{Int, N} where N)
true

julia> 1 <: Any
ERROR: TypeError: issubtype: expected Type, got Int64

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 26, 2018, 4:29am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/3 "2018-02-26T04:29:39Z")

</div>

What’s the syntax to constrain `N` to be of type `Int` for example?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 26, 2018, 5:55am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/4 "2018-02-26T05:55:48Z")

</div>

AFAICT there is no such syntax. You can verify constraints like that in inner constructors.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [February 26, 2018, 6:23am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/5 "2018-02-26T06:23:57Z")

</div>

They are equivalent.

```julia
foo(bar::Baz{T}) where T

```

defaults to `T<:Any`.  
I believe either style is fine. What is discouraged (and wrong in my opinion) is to leave it `Any`, when in reality it should be something else, (e.g., `Number`, `Real`, etc.)

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [February 26, 2018, 6:33am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/6 "2018-02-26T06:33:09Z")

</div>

@xiaodai, do you refer to

```julia
function magic(obj::AbstractVector{T}) where T <: Integer
    total = zero(T)
    for each ∈ obj
        total += each
    end
    return total
end

```

This allows methods for all subtypes of `Integer`.  
If you want only to establish methods for `Int64` use

```julia
function magic(obj::AbstractVector{T}) where T<:Int64
    total = zero(T)
    for each ∈ obj
        total += each
    end
    return total
end

```

However, since it is only defined for `Int64` I could define it equivalently as

```julia
function magic(obj::AbstractVector{<:Int64})
    total = zero(Int64)
    for each ∈ obj
        total += each
    end
    return total
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 26, 2018, 7:44am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/7 "2018-02-26T07:44:00Z")

</div>

> [@Nosferican](#):
>
> `where T<::Int64`

Why would one do this? `Int64` is a concrete type. Same applies to `<:Int64`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 26, 2018, 9:05am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/8 "2018-02-26T09:05:02Z")

</div>

I think he’s referring to restricting `N` to be a _value_ of type `Int`, not a type that is a subtype of `Integer`.

As in `Array{Int, 2}`, where the first type parameter is a subtype of `Integer`, but the second parameter is the value `2`.

> [@Nosferican](#):
>
> `function magic(obj::AbstractVector{<:Int64})`

This should just use `AbstractVector{Int64}`.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [February 26, 2018, 9:17am UTC](https://discourse.julialang.org/t/is-where-t-the-same-as-where-t-any-in-all-cases/9329/9 "2018-02-26T09:17:42Z")

</div>

Aye, the cases are just to demonstrate the general syntax. If one needs to implement a method for a concrete type, generally one should dispatch on abstract types and then specialize on concrete if one needs to or for multiple dispatch. For concrete types, one doesn’t need to use the `T` `where` syntax as there is no ambiguity and one can hardcode the necessary information for type-stability. For example,

```julia
function magic(obj::AbstractVector{Int64})
    total = zero(Int64)
    for each ∈ obj
        total += each
    end
    return total
end

```
