Hi. i have a type that is not a subtype of AbstractArray
, but it has a notion of size:
#basically, vector + metadata
struct SingleParam{T,M}
names::Vector{String}
values::Vector{T} #same size as names
metadata::M
end
#a constructor that only accepts values. normally the constructor requires names and values to be the same size, but this code is only for demonstration purposes
function SingleParam(values::Vector{T})
names = [string(i) for i in 1:length(values)]
return SingleParam{T,Nothing)(names,values,nothing)
end
#same size as the underlying values vector
Base.size(param::SingleParam) = Base.size(param.values)
Base.broadcastable(param::SingleParam) = param.values
Base.BroadcastStyle(::Type{<:SingleParam}) = Broadcast.Style{SingleParam}()
and i want to perform implace operations over said struct:
p = SingleParam(rand(5))
p .= exp.(p) .+ sin.(p) .+ rand(5)
one way i found to do this is by defining copyto!
function copyto!(dest::SingleParam,src)
copyto!(dest.values,src)
end
function copyto!(dest::SingleParam,SingleParam)
copyto!(dest.values,src.values)
return dest
end
but this does not appear to be the official way. as all the documentation does not seem to cover this case. furthermore, seems that this can cause troubles:
and it appears to be an ambiguous method (in conjunction with StaticArrays):
copyto!(dest::MyModule.SingleParam, src) in MyModule at C:\Users\user\.julia\dev\MyModule\src\database\params\SingleParam.jl:61,
copyto!(dest, B::Base.Broadcast.Broadcasted{<:StaticArraysCore.StaticArrayStyle}) in StaticArrays at C:\Users\user\.julia\packages\StaticArrays\NOLon\src\broadcast.jl:65)
so my question is, what is the correct way to allow this type of implace operations?