# Zero-sized array of parametric types

**URL:** <https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [August 28, 2017, 6:56am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609 "2017-08-28T06:56:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [August 28, 2017, 6:56am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/1 "2017-08-28T06:56:29Z")

</div>

I have a Parametric type `mytype{N}`. One of its field is an `SVector`, and I want to have no pointers.

I use `Vectors` of `mytype{N}`. And of course, there comes a time when I have a vector `v` of length 0 of `mytype{N}`.

Then I do  
`v[:] = 0.`  
and boom:

```julia
MethodError: Cannot `convert` an object of type Float64 to an object of type mymodule.mytype
  This may have arisen from a call to the constructor mymodule.mytype(...),
  since type constructors fall back to convert methods.

```

I have written a `Float64` to `mytype{n}` converter, but _not_ a `Float64` to `mytype` converter.

My interpretation: because the array is empty, the compiler is unable to determine `N`, tries to call a non-existent converter, and errors.

I have several questions:

- The empty vector was built with data on `N` available (see below), and the compiler is not keeping track, isn’t that a weakness?  
`variate(a::Vector) = [mytype{size(a,1)}(whatever) for i=1:size(a,1)]`
- why need a converter if zero data is to be converted, isn’t that a potential improvement of Julia?
- If I wrote a `Float64` to `mytype` converter, how does it look like (I don’t know `N`, right?). Is that the idiomatic way out?
- I have my workaround (if length is zero, do not initialise…), but this jars with Julia’s natural elegance… Any suggestion?

---

<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:** [August 28, 2017, 6:59am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/2 "2017-08-28T06:59:10Z")

</div>

To me, it is a bit hard to understand the question. It is usually good if the code you provide is fully contained and can be run by pasting it in the REPL, i.e. give the definition of `mytype` how you construct it and the code you run for the error message to appear.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [August 28, 2017, 7:50am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/3 "2017-08-28T07:50:03Z")

</div>

OK, trying to reproduce the issue in a compact code, I realise the depth of my confusion, because I generate other errors I cannot handle. So, in Julia 6.0:

```julia
using StaticArrays
mutable struct mytype{N}
    plain :: SVector{N,Float64}
end   

mytype{N}(x::Float64) = mytype{N}([x for i = 1:N])
convert{N}(::Type{mytype{N}},x::Float64) = mytype{N}(x)
promote_rule{N}(::Type{mytype{N}},::Type{Float64}) = mytype{N}

#v = [mytype{3}(Float64(i)) for i = 1:1]
v = [mytype{3}(Float64(i)) for i = 1:0

v[:] = 0.

```

This causes  
`LoadError: MethodError: Cannot `convert` an object of type Float64 to an object of type mytype{3}`  
What’s wrong with my convertor?

Thanks beforehand

Philippe

---

<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:** [August 28, 2017, 8:28am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/4 "2017-08-28T08:28:15Z")

</div>

The code you posted doesn’t run as is on 0.6 (try copy paste it).

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [August 28, 2017, 8:35am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/5 "2017-08-28T08:35:53Z")

</div>

Indeed I forgot a bracket on the second-last line, apologies. With that correction, I get the “convert” error.

---

<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:** [August 28, 2017, 8:44am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/6 "2017-08-28T08:44:46Z")

</div>

Nope, you get an error message about not importing `convert`.

Making a minimal effort to come up with a MWE that exhibits your problem often gets you help quicker.

In the form above, it is still hard to make sense of your code. What are you trying to do? Since `v` is an empty array of `mytype` (NB: please capitalize type names correcly), what should `v[:] = 0` achieve?

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [August 28, 2017, 8:54am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/7 "2017-08-28T08:54:31Z")

</div>

Interesting, thank you.

I understand that since I overload convert, I should import it, and that is going to be a source of trouble.

Interestingly, I do NOT get the same error message as you do, and that is queer.

The code is just intended to trigger a given error, it makes no sense in itself. Having general algorithms that need to cope with “zero operations” on empty array is not that uncommon…

So, running

```julia
import Base.convert,Base.promote_rule
using StaticArrays
mutable struct mytype{N}
    plain :: SVector{N,Float64}
end   

mytype{N}(x::Float64) = mytype{N}([x for i = 1:N])
convert{N}(::Type{mytype{N}},x::Float64) = mytype{N}(x)
promote_rule{N}(::Type{mytype{N}},::Type{Float64}) = mytype{N}

#v = [mytype{3}(Float64(i)) for i = 1:1]
v = [mytype{3}(Float64(i)) for i = 1:0]

v[:] = 0.

```

gives me

```julia
WARNING: Method definition 
LoadError: MethodError: Cannot `convert` an object of type Float64 to an object of type StaticArrays.SArray{Tuple{3},Float64,1,3}
map(Any, Union{SA<:(StaticArrays.StaticArray{S, T, N} where N where T where S<:Tuple), AbstractArray{T, N} where N where T}...) in module StaticArrays at C:\Users\philippem\.julia\v0.6\StaticArrays\src\mapreduce.jl:10 overwritten in module StaticArrays at C:\Users\philippem\.julia\v0.6\StaticArrays\src\mapreduce.jl:10.
WARNING: static parameter N does not occur in signature for Type at untitled:8.
The method will not be callable.

```

So my failure to import was not the only problem…

Any thoughts?

---

<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:** [August 28, 2017, 9:00am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/8 "2017-08-28T09:00:06Z")

</div>

You also get:

```julia
WARNING: static parameter N does not occur in signature for Type at REPL[30]:1.
The method will not be callable.

```

from the `mytype{N}` function. Try:

```julia
mytype{N}(x::Float64) where {N} = mytype{N}([x for i = 1:N])

```

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [August 28, 2017, 9:09am UTC](https://discourse.julialang.org/t/zero-sized-array-of-parametric-types/5609/9 "2017-08-28T09:09:04Z")

</div>

Ååååh takk så mycket!

My method was not syntactically correct, so I had no converter…

So simple. So I am going study the “where” manual in details…

Thank you!!!
