-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add get_in/1 with safe nil-handling for access and structs #13370
Conversation
assert get_in(is_nil.age) == nil | ||
|
||
assert_raise KeyError, ~r"key :unknown not found", fn -> get_in(users.unknown) end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably redundant but I'd consider adding:
end | |
assert_raise KeyError, ~r"key :unknown not found", fn -> get_in(users.meg.unknown) end | |
end |
@josevalim, would it be feasible to add list handling too if these kinds of changes are on the table? I've been doing something like this a little bit as a utility:
|
It has been discussed a couple times. Doing We have discussed subset operators, inspired by |
I think putting such access would make the most sense in a helper like get_in though, not dedicated syntax. Consider the scenario of a user trying to use get_in with a map that contains nested lists. They'll end up decomposing it into a pipeline that calls Enum.at anyways, cause that's what they wanted to do. |
@srcrip that would be a departure of how Your proposed mechanism would not extensible either (because it would be hardcoded as part of get_in) and the rationale of Access is exactly to make the syntax extensible to everyone. What happens if someone wants to provide a similar behavior for, say, Nx.Tensor? |
Co-authored-by: Leandro Pereira <leandro@leandro.io>
Co-authored-by: Leandro Pereira <leandro@leandro.io>
Btw, I totally hear you and I agree. However, we need to find a solution that is consistent with the rest of the language. I am not sure at the moment what that is, but let's continue exploring! |
💚 💙 💜 💛 ❤️ |
No description provided.