# How to construct parametric type with SharedArray or Array, depending on args to constructor?

**URL:** <https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 23, 2016, 9:56am UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517 "2016-11-23T09:56:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 23, 2016, 9:56am UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/1 "2016-11-23T09:56:32Z")

</div>

I would like to construct a parametric type with `SharedArrays` whenever `nprocs()>1`, and with standard `Array`s when not. I’m trying to understand why this doesnt work:

```julia
type V{T}
   array :: T
   function V{T}(arr::T)
       this = new()
       if isa(arr,SharedArray)
            this.array = SharedArray(Float64,(2,2),pids=workers())
       else
            this.array = zeros(2,2)
       end
       return this
    end
end
ERROR: MethodError: Cannot `convert` an object of type SharedArray{Float64,0} to an object of type V{T}
This may have arisen from a call to the constructor V{T}(...),
since type constructors fall back to convert methods.
 in V{T}(::SharedArray{Float64,0}) at ./sysimg.jl:53

```

where my idea was to say

```julia
s = zeros(2,2)
if nprocs()>1
    s = SharedArray{Float64}()
end
# construct
V(s)

```

---

<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:** [November 23, 2016, 10:26am UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/2 "2016-11-23T10:26:51Z")

</div>

You could try putting the conditional in an outer constructor:

```julia
type MaybeShared{T <: AbstractArray}
    array::T
end

function MaybeShared(T=Float64, dims=(2,2))
    MaybeShared(nprocs() == 1 ?
                Array{T}(dims...) : SharedArray(T,dims,pids=workers()))
end

MaybeShared() # gives you the array

```

Also, maybe there is some context missing, but I am not sure I understand what you gain by wrapping the array. You could simply have a function that returns the array of the desired type.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 23, 2016, 10:33am UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/3 "2016-11-23T10:33:59Z")

</div>

yes, that works! thanks.  
I need those (many) arrays to be members of a parametric type to be able to organize the workflow better. that’s why I don’t just use a function that spits out a bunch of arrays.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 23, 2016, 9:31pm UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/4 "2016-11-23T21:31:20Z")

</div>

Wrapper types should be declared as `immutable`.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 24, 2016, 8:35am UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/5 "2016-11-24T08:35:01Z")

</div>

not if I have to set fields of that type later on.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 24, 2016, 3:59pm UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/6 "2016-11-24T15:59:31Z")

</div>

If there are more fields in that type than the one referencing the wrapped array (i.e. more than in the provided code), then it’s not a “wrapped array” for “workflow organization”, but it’s a whole separate type with its own **state** (the extra fields), which then has to be mutable. I’m stating the distinction not for theoretical purity, but because it may reveal design defects.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 24, 2016, 4:09pm UTC](https://discourse.julialang.org/t/how-to-construct-parametric-type-with-sharedarray-or-array-depending-on-args-to-constructor/517/7 "2016-11-24T16:09:43Z")

</div>

“workflow organization” is the case for me here.
