# Parametric types with parameters left unspecified as struct fields

**URL:** <https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [November 3, 2017, 7:34pm UTC](https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865 "2017-11-03T19:34:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [November 3, 2017, 7:34pm UTC](https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865/1 "2017-11-03T19:34:37Z")

</div>

Suppose I have a mutable struct with a field used to store a sparse matrix, e.g.

```julia
mutable struct myStruct{Tv<:Number,Ti<:Integer}
    A::SparseMatrixCSC{Tv,Ti}
end

```

This works as expected for various combinations of `Tv` and `Ti`. Now suppose I was lazy, as I was yesterday and instead I defined

```julia
mutable struct mySimpleStruct
    A::SparseMatrixCSC
end

```

I understand that this is bad practice because the actual concrete type of `A` in instances of `mySimpleStruct` can’t be inferred. However, I had assumed that the `mySimpleStruct` construction would still allow me to work with any type of sparse matrix. However, I’ve observed the following behaviour, which I don’t understand:

```julia
>A = sprandn(10,10,0.1)
>A = convert(SparseMatrixCSC{Float64,Int32},A)
>mySimpleStruct(A)

  mySimpleStruct
  A → 10×10 SparseMatrixCSC{Float64,Int64} with 7 stored entries

```

As you can see, in storing my `SparseMatrixCSC{Float64,Int32}` matrix in a `mySimpleStruct`, the non-zero index storage was converted from `Vector{Int32}` to `Vector{Int64}`. I would have thought that specifying a field of a struct as just `SparseMatrixCSC` would allow it to store any type of sparse matrix but this doesn’t seem to be the case. Anyone know why the type conversion shown above is happening? I would have thought `SparseMatrixCSC` would behave like an abstract type with all the various concrete types `SparseMatrixCSC{Tv,Ti}` being subtypes. Why is that not the case? `SparseMatrixCSC{Float64,Int32} <: SparseMatrixCSC` returns `true`.

Thanks, Patrick

---

<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:** [November 3, 2017, 7:50pm UTC](https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865/2 "2017-11-03T19:50:01Z")

</div>

Constructing a `struct` (with all of its parameters specified) or storing an element will, as I understand it, implicitly call `convert(<the type of the field>, <the value>)`. If `x isa T`, then `convert(T, x)` is generally a no-op so you don’t see this behavior. However, `SparseMatrixCSC` is a bit odd in that respect:

```julia
julia> A = sprandn(10, 10, 0.1);

julia> A32 = convert(SparseMatrixCSC{Float64, Int32}, A);

julia> typeof(A32)
SparseMatrixCSC{Float64,Int32}

julia> typeof(convert(SparseMatrixCSC, A32))
SparseMatrixCSC{Float64,Int64}

```

this happens because of a method in `sparsematrix.jl`:

```julia
julia> @edit convert(SparseMatrixCSC, A32)

# should show the following in your editor:
# convert(::Type{SparseMatrixCSC}, M::AbstractMatrix{Tv}) where {Tv} = convert(SparseMatrixCSC{Tv,Int}, M)

```

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [November 3, 2017, 7:53pm UTC](https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865/3 "2017-11-03T19:53:40Z")

</div>

Oh, interesting. That explains it. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 3, 2017, 7:56pm UTC](https://discourse.julialang.org/t/parametric-types-with-parameters-left-unspecified-as-struct-fields/6865/4 "2017-11-03T19:56:09Z")

</div>

Arguably this is a bug. Probably this should be defined

```julia
convert(::Type{SparseMatrixCSC}, M::SparseMatrixCSC) = M

```
