Skip to content
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

2.0: Make NamedTuples not indexable by symbol #50375

Open
jariji opened this issue Jun 30, 2023 · 2 comments
Open

2.0: Make NamedTuples not indexable by symbol #50375

jariji opened this issue Jun 30, 2023 · 2 comments
Labels
breaking This change will break code
Milestone

Comments

@jariji
Copy link
Contributor

jariji commented Jun 30, 2023

Generally, NamedTuples support the interfaces of both structs and tuples. But neither structs nor tuples support indexing by symbol. It may be convenient for situations with a non-identifier property name, but it mixes the semantics of getproperty and getindex confusingly by dispatching on the argument type.

julia> NamedTuple([Symbol("2") => 20, :a => 10])[Symbol("2")]
20

julia> NamedTuple([Symbol("2") => 20, :a => 10])[2]
10

getproperty should not decide its semantics based on the type of the argument --- for different behavior, use a different function. Imho, getindex(::NamedTuple, i) should index positions and getproperty should go by property name. These should be kept separate.

@adienes
Copy link
Member

adienes commented Jun 30, 2023

notably: would resolve basically all of these annoying inconsistencies #49932

@oscardssmith oscardssmith added this to the 2.0 milestone Jun 30, 2023
@oscardssmith oscardssmith added the breaking This change will break code label Jun 30, 2023
@rafaqz
Copy link
Contributor

rafaqz commented Aug 27, 2023

The major drawback of this is that getindex has much better syntax for using NamedTuple for practical tasks when the key is a variable: my_nt[k] vs getproperty(my_nt, k). I agree with most of #49932 but there is no reason we cant change eachindex and keep getindex - Int also works in getindex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code
Projects
None yet
Development

No branches or pull requests

4 participants