I like how @with_kw
can construct new type instances based on an existing one, and was successful with it until I made my struct parametric. First, the non-parametric version:
using Parameters
abstract type SomeTypes end
makeinstance(mine::M; moreparms...) where M<:SomeTypes = M(mine; moreparms...)
@with_kw struct AnyType <: SomeTypes # lacking parametric types
a = 1.
b = 2.
end
m = AnyType();
makeinstance(m; b=3.) # works fine to change field b
But when the struct is parametric, the same thing works in the REPL but fails in the function:
@with_kw struct SameType{T} <: SomeTypes # with parametric type T
a::T = 1.
b::T = 2.
end
n = SameType();
julia> SameType(n; b=3.) # optionally changing b works in REPL
SameType{Float64}
a: Float64 1.0
b: Float64 3.0
julia> makeinstance(n; b=3.) # but not in a function! Why?
ERROR: MethodError: no method matching SameType{Float64}(::SameType{Float64}; b::Float64)
Closest candidates are:
SameType{T}(::Any, ::Any) where T got unsupported keyword argument "b"
@ Main ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:503
SameType{T}(; a, b) where T
@ Main ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:491
Stacktrace:
[1] makeinstance(mine::SameType{Float64}; moreparms::@Kwargs{b::Float64})
@ Main ./REPL[8]:1
[2] top-level scope
@ REPL[16]:1
I don’t understand the MethodError
, and why this doesn’t work parametrically. I’d appreciate any suggestions how to get this to work within the function. Thanks!