Skip to content

Immutable FieldVectors from StaticArrays.jl #19

Description

@ranocha

SArray from StaticArrays works fine, but using a custom immutable type which is a subtype of FieldVector, recursivecopy! fails.

using StaticArrays
using RecursiveArrayTools

a = [SVector(1.,2.)]
b = similar(a)
recursivecopy!(b, a)


struct Bad <: FieldVector{2,Float64}
    a::Float64
    b::Float64
end

a = [Bad(1.,2.)]
b = similar(a)
recursivecopy!(b, a)

This results in

type Bad is immutable

Stacktrace:
 [1] copy!(::Bad, ::Bad) at ./multidimensional.jl:805
 [2] recursivecopy!(::Array{Bad,1}, ::Array{Bad,1}) at /.../.julia/v0.6/RecursiveArrayTools/src/utils.jl:21
 [3] include_string(::String, ::String) at ./loading.jl:515

According to the documentation of StaticArrays.jl

it is also worth noting that FieldVectors may be mutable or immutable

Thus, the following works fine.

mutable struct NotBad <: FieldVector{2,Float64}
    a::Float64
    b::Float64
end
a = [NotBad(1.,2.)]
b = zeros(a)
recursivecopy!(b, a)

I came across this while I was using OrdinaryDiffEq.jl with u of the form [Bad(1.,2.)]. However, this has been working some months ago, see SciML/OrdinaryDiffEq.jl#50.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions