hello, how to define NamedTuple type with variable number of elements?
(:a, :b) isa NTuple{N,Symbol} where N # -> true
(1, 2) isa NTuple{N,Int} where N # -> true
why ?
(a=1,b=2) isa NamedTuple{NTuple{N,Symbol}, NTuple{N,Int}} where N # -> false
1 Like
A NTuple
is not the same as a NamedTuple
.
julia> (a=1,b=1) |> typeof
NamedTuple{(:a, :b), Tuple{Int64, Int64}}
julia> NamedTuple{(:a,:b)}((1,1))
(a = 1, b = 1)
1 Like
Dan
December 28, 2022, 12:31pm
3
To make @fatteneder 's remark perhaps clearer:
NamedTuple parameters are not types! They are actual tuples.
So: NTuple{N,Symbol} where N
is a type, and not the needed parameter for the NamedTuple.
1 Like
but
Tuple{Int, Int} <: NTuple{N,Int} where N # -> true
Tuple{Symbol, Symbol} <: NTuple{N,Symbol} where N # -> true
Dan
December 28, 2022, 12:44pm
5
The closest to what you wrote I got is:
(a=1,b=2) isa NamedTuple{keys, <:Tuple{Vararg{Int,N}}} where {keys, N} # true
Note:
(:a, :b) <: NTuple{N,Symbol} where N # error
(:a, :b) isa NTuple{N,Symbol} where N # true
1 Like
Dan
December 28, 2022, 1:00pm
7
thanks too. I always learn nuances of Julia from thinking about such questions