-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inconsistent handling of abstract types in NamedTuple #34547
Comments
Ref #32699, which is about making abstractly inferred NamedTuples useful |
Making The
The NamedTuple constructor behaves as we need:
i.e. if you request a NamedTuple with abstract element types you get one with that exact (concrete) type. This is not useless, but in fact essential for tabular data with missings. |
Whoops, I forgot the parentheses in the constructor invocation! You're correct that this works with abstract types, but not with julia> NamedTuple{(:a,:b), <:Tuple{Number,Number}}((3,4))
ERROR: MethodError: no method matching NamedTuple{(:a, :b),#s1} where #s1<:Tuple{Number,Number}(::Tuple{Int64,Int64}) similar to this which already works: julia> NamedTuple{(:a,:b), <:Tuple}((3,4))
(a = 3, b = 4) ? But we should be able to make |
|
Anyway, closing this issue since it looks like making julia> NamedTuple{(:x,:y), Tuple{Integer,Number}}((x=3,y=4))
NamedTuple{(:x, :y),Tuple{Integer,Number}}((3, 4)) |
I noticed the following seeming inconsistency:
You can use
<:Tuple
for the second argument for subtyping, but even that doesn't work as a constructor:(Of course,
either. Correction:Tuple{Number,Number}(3,4)
doesn't workTuple{Number,Number}((3,4))
is the correct spelling and works.)This makes it a bit hard to use a
NamedTuple
declaration with abstract types for the fields. Proposal:(a=3,b=4) isa NamedTuple{(:a,:b), Tuple{Number,Number}}
should returntrue
, and in general it should work likeisa Tuple{...}
.Tuple{Number,Number}
andNamedTuple{(:a,:b), Tuple{Number,Number}}
should also work as constructors, taking the concrete type from their argument types.In theory these are breaking, but in practice I think it should qualify as a minor change.
NamedTuple
with abstract types is basically useless right now, and (2) throws an exception, so it is hard to imagine any working code depending on the current behaviors.The text was updated successfully, but these errors were encountered: