I have a circle type
struct Circle{T <: Real}
# radius is a subtype of
# the real number approximation types
R::T
# The center is a named pair of real
# number approximation types
C::NamedTuple{T, T}
end
which I would like to instantiate with
circles = NamedTuple(
A = Circle(1, ( 1, 1)),
B = Circle(1, ( 2, 2))
)
which produces the error
ERROR: LoadError: MethodError: no method matching Circle(::Int64, ::Tuple{Int64,Int64})
Closest candidates are:
Circle(::T<:Real, ::NamedTuple{T<:Real,T<:Real}) where T<:Real
Why does this error occur and what am I doing wrong?
Named tuples are parametrized by names and a tuple:
julia> struct Circle{T <: Real}
R::T
C::NamedTuple
Circle(r::T, c::NamedTuple{N,Tuple{T,T}}) where {N, T <: Real} = new{T}(r,c)
end
julia> Circle(1, (a=1, b=2))
Circle{Int64}(1, (a = 1, b = 2))
If you want to construct a Circle
with a tuple, you can provide a default mapping:
julia> function Circle(r::T, c::Tuple{T,T}) where {T <: Real}
a, b = c
return Circle(r, (a=a, b=b))
end
Circle
julia> Circle(1, (1, 2))
Circle{Int64}(1, (a = 1, b = 2))
If you don't need named tuples, you can use regular tuples:
julia> struct Circle{T <: Real}
R::T
C::Tuple{T, T}
end
julia> Circle(1, (1, 2))
Circle{Int64}(1, (1, 2))