# Matrix{Float64}\<:Matrix{Number} is false?

**URL:** <https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077>\
**Category:** General Usage\
**Created:** [December 1, 2022, 5:42am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077 "2022-12-01T05:42:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)\
**Post date:** [December 1, 2022, 5:42am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/1 "2022-12-01T05:42:35Z")

</div>

Why is `Matrix{Float64}<:Matrix{Number}` false?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 1, 2022, 5:43am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/2 "2022-12-01T05:43:18Z")

</div>

search type invariance. TLDR is that they need different memory representations.

---

<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:** [December 1, 2022, 6:01am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/3 "2022-12-01T06:01:53Z")

</div>

It also follows from the rules of concrete subtyping. `Matrix{Number}` is a concrete type that can be instantiated, and concrete types cannot have subtypes.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 1, 2022, 6:11am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/4 "2022-12-01T06:11:21Z")

</div>

Try this:

```julia
Matrix{Float64} <: Matrix{<:Number}

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 1, 2022, 6:24am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/5 "2022-12-01T06:24:34Z")

</div>

See also [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types)

In particular:

> In other words, in the parlance of type theory, Julia’s type parameters are _invariant_, rather than being [covariant (or even contravariant)](https://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29). This is for practical reasons: while any instance of `Point{Float64}` may conceptually be like an instance of `Point{Real}` as well, the two types have different representations in memory:
> 
> - An instance of `Point{Float64}` can be represented compactly and efficiently as an immediate pair of 64-bit values;
> - An instance of `Point{Real}` must be able to hold any pair of instances of [`Real`](https://docs.julialang.org/en/v1/base/numbers/#Core.Real). Since objects that are instances of `Real` can be of arbitrary size and structure, in practice an instance of `Point{Real}` must be represented as a pair of pointers to individually allocated `Real` objects.
> 
> The efficiency gained by being able to store `Point{Float64}` objects with immediate values is magnified enormously in the case of arrays: an `Array{Float64}` can be stored as a contiguous memory block of 64-bit floating-point values, whereas an `Array{Real}` must be an array of pointers to individually allocated [`Real`](https://docs.julialang.org/en/v1/base/numbers/#Core.Real) objects – which may well be [boxed](https://en.wikipedia.org/wiki/Object_type_%28object-oriented_programming%29#Boxing) 64-bit floating-point values, but also might be arbitrarily large, complex objects, which are declared to be implementations of the `Real` abstract type.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 1, 2022, 6:46am UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/6 "2022-12-01T06:46:50Z")

</div>

[https://m3g.github.io/JuliaNotes.jl/stable/typevariance/](https://m3g.github.io/JuliaNotes.jl/stable/typevariance/)

---

<div class="post-metadata">

**Author:** ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)\
**Post date:** [December 1, 2022, 7:57pm UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/7 "2022-12-01T19:57:46Z")

</div>

Could you elaborate on this a bit more? I am confused by the syntax of `Matrix{<:Number}`

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [December 1, 2022, 8:24pm UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/8 "2022-12-01T20:24:27Z")

</div>

Invariance of type parameters means that e.g. `Matrix{Float64}` is not a subtype of `Matrix{Number}`, even if `Float64` is a subtype of `Number`. However, there exists “wildcard” type parameters in the form of `UnionAll` types. Typically such a `UnionAll` type is written with the `where` clause: `Matrix{T} where T<:Number`. It is an abstract type which has all `Matrix{T}` as subtypes where `T` is a subtype of `Number`. A short form is `Matrix{<:Number}`, which can be used if you do not need to use the type variable `T` elsewhere.

```julia
julia> dump(Matrix{<:Number})
UnionAll
  var: TypeVar
    name: Symbol #s3
    lb: Union{}
    ub: Number <: Any
  body: Array{var"#s3"<:Number, 2} <: DenseArray{var"#s3"<:Number, 2}

julia> dump(Matrix{T} where T<:Number)
UnionAll
  var: TypeVar
    name: Symbol T
    lb: Union{}
    ub: Number <: Any
  body: Array{T<:Number, 2} <: DenseArray{T<:Number, 2}

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 1, 2022, 8:26pm UTC](https://discourse.julialang.org/t/matrix-float64-matrix-number-is-false/91077/9 "2022-12-01T20:26:35Z")

</div>

Beat me to it 😉

This was going to be my response:

As others have mentioned, `Matrix{Number}` is a concrete type, and one concrete type cannot subtype another concrete type.

`Matrix{<:Number}` is a union of types, and is shorthand for `Matrix{T} where {T<:Number}`.
