# Type Parameterization of Elements based on \`FixedPointNumber.jl\` in a Function

**URL:** https://discourse.julialang.org/t/type-parameterization-of-elements-based-on-fixedpointnumber-jl-in-a-function/88636
**Category:** General Usage
**Tags:** question, type, parametric-types, fixed-point
**Created:** [October 12, 2022, 9:33pm UTC](https://discourse.julialang.org/t/type-parameterization-of-elements-based-on-fixedpointnumber-jl-in-a-function/88636 "2022-10-12T21:33:47Z")
**Posts on this page:** 1
**Showing post:** 11

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 13, 2022, 10:19am UTC](https://discourse.julialang.org/t/type-parameterization-of-elements-based-on-fixedpointnumber-jl-in-a-function/88636/11 "2022-10-13T10:19:13Z")

</div>

The type system can get confusing at times. So thanks for the question which got me to learn a few things. Good sources I’ve found:  
[https://docs.julialang.org/en/v1/manual/types/#man-typet-type](https://docs.julialang.org/en/v1/manual/types/#man-typet-type)  
and

> [@What is difference between Type{T} and T](https://discourse.julialang.org/t/what-is-difference-between-type-t-and-t/19325):
>
> Hello everyone, I have just started to learn about Julia(\<:1.0). Unfortunately, I am not able to understand the use/functionality of Type{T}, for example, in convert(::Type{T}, a::T) where {T\<:AbstractArray} = a why cannot we use convert(::T, a::T) where {T\<:AbstractArray} = a please excuse me if this question is too basic.

As for the specific cases:

> [@RoyiAvital](#):
>
> ```julia
> Matrix{RGB{N0f8}} <: Matrix{RGB{<: Normed{U}}} where{U <: Unsigned}
> false
> 
> ```

Here it is important to note Julia abstract types are _invariant_, which means:

```julia
julia> Vector{Float64} <: Vector{Real}                                                                                   
false

```

even though `Float64 <: Real`. In this example:

```julia
N0f8 <: Normed{U} where {U} # true. but...
N0f8 == Normed{U} where {U} # false

```

which is the same situation as earlier with `Float64` and `Real`.

Next,

> [@RoyiAvital](#):
>
> ```julia
> julia> Matrix{RGB{N0f8}} <: Matrix{RGB{Normed{U}}} where{U <: Unsigned}
> false
> 
> ```

Omitting a parameter `T` is the same as adding a `{T}` to `where` clause, and thus:

```julia
( Normed{U} where {U} ) == ( Normed{U,T} where {U,T} ) # true

```

The above can be rewritten as:

```julia
Matrix{RGB{N0f8}} <: Matrix{RGB{Normed{U,T}}} where{U <: Unsigned,T}

```

`N0f8` is an alias to `Normed{UInt8, 8}` and so we can rewrite:

```julia
Matrix{RGB{Normed{UInt, 8}} <: Matrix{RGB{Normed{U,T}}} where{U <: Unsigned,T}

```

Again by _invariance_ this is false.

The next case:

> [@RoyiAvital](#):
>
> ```julia
> julia> Type{Matrix{RGB{N0f8}}} isa Matrix{RGB{Normed{UInt8, 8}}}
> false
> 
> ```

is false because the type of value before `isa` is not the type after `isa`, in fact it is the other way around and:

```julia
julia> Matrix{RGB{N0f8}} isa Type{Matrix{RGB{Normed{UInt8, 8}}}}                                                         
true                                                                                                                     

```

In general `T isa Type{T}` and `Type{T}` is the type-of-the-type. This is useful to force parameters of parametric functions to be types (see the references at the top).

Lastly, getting back to first case:

> [@RoyiAvital](#):
>
> ```julia
> julia> Matrix{RGB{N0f8}} <: Matrix{<: RGB{<: Normed{U}}} where{U <: Unsigned}
> true
> 
> ```

The `{<: type} where {...}` notation is syntactic-sugar for `{T} where {..., T <: type}`. Armed with this knowledge rewriting the second term gives:

```julia
Matrix{<:RGB{<:Normed{U}}} where {U<:Unsigned} == 
  Matrix{S} where {U <: Unsigned, S <: RGB{T} where {T<:Normed{U}}}

```

Now we can more easily see that:

```julia
Matrix{RGB{N0f8}} <: 
  Matrix{S} where {U <: Unsigned, S <: RGB{T} where {T<:Normed{U}}}

```

since `S = RGB{N0f8}` and  
indeed `S <: RGB{T} where {T<:Normed{U}}`  
because `RGB{N0f8} <: RGB{T} where {T<:Normed{U}}`  
with `T = N0f8` because `N0f8 <: Normed{U}`  
by `N0f8 = Normed{UInt8, 8}`  
and `Normed{UInt8, 8} <: Normed{U}`  
with `U = UInt8`  
and `Normed{UInt8, 8} <: Normed{UInt8}`  
by missing parameters considered “free” so `Anytype{S,T} <: Anytype{S}`.

It’s all clear as mud… hopefully this discussion diluted this mud a bit 😜

---

_[View the full topic](https://discourse.julialang.org/t/type-parameterization-of-elements-based-on-fixedpointnumber-jl-in-a-function/88636)._
