-
Notifications
You must be signed in to change notification settings - Fork 22
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
Possible to match a NamedTuple? #95
Comments
had a similar need and found the following work-around works nt = (; a=1, b=2)
NT = typeof(nt)
@match (; a, b) = nt begin
NT(1, b) => begin
# you have access to locals a = 1 and b = 2 here
end
end |
@xukai92 It looks like this only works for a single type of named tuple. Could be handy, but definitely not a complete solution. |
yeah but |
I had that thought as well, but I'm not sure how you would handle a case where you want to match on a named tuple that could have different types, e.g. nt = rand((
(;a=1, b=2),
(x = "x", y="z")
))
@match nt begin
(;a=1, b) => begin
@show a b
end
(;x, y) => begin
@show x y
end
end |
i guess in that case you will have to do something even more messy (deconstructing in the match statements) nts = (
(; a=1, b=2),
(; x="x", y="z"),
)
NT1, NT2 = typeof.(nts)
nt = rand(nts)
@match nt begin
NT1(1, b) => begin
(; a, b) = nt
@show a b
end
NT2(x, y) => begin
(; x, y) = nt
@show x y
end
end |
I didn't see this in the docs so I assume it's not possible. It would be really nice to be able to match NamedTuples, e.g.
The text was updated successfully, but these errors were encountered: