You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was using attrs in Python. It shares a similar functionality as Parameters.jl. However, one thing it has is not supported by Parameters.jl now---the default converter. It is often the case that users do not supply the right type of parameters to the constructor. I want that we can handle this if they do not give the right type instead of throwing an error.
Here is an example:
struct A
mass::Int
velocity::String
coordinate::Vector{Pair{Int, Float64}}
time::NTuple{3, Float64}
frequency::Float64end
For some reason, I want to handle if a user supplies a Vector{Float64} or a NTuple{N,Float64} to the coordinate field, the construct A could directly converts it to a Vector{Pair{Int, Float64}}, by calling a function
convertfield(x::Vector{Pair{Int, Float64}}) = x
functionconvertfield(x::Union{AbstractVector{<: Real}, NTuple{N, <: Real} where {N}})
returncollect(pairs(Float64.(x)))
end
I was using
attrs
in Python. It shares a similar functionality asParameters.jl
. However, one thing it has is not supported byParameters.jl
now---the default converter. It is often the case that users do not supply the right type of parameters to the constructor. I want that we can handle this if they do not give the right type instead of throwing an error.Here is an example:
For some reason, I want to handle if a user supplies a
Vector{Float64}
or aNTuple{N,Float64}
to thecoordinate
field, the constructA
could directly converts it to aVector{Pair{Int, Float64}}
, by calling a functionon it. The syntax could be like
where
@convert
is a macro that takes a function. I could write something likeBut it is just a default value, not a converter.
The text was updated successfully, but these errors were encountered: