Chapter 1: Explicitly explain switching on enums and tagged structs #112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When I encountered the tagged enums, I was confused about the
.a
syntax in the switch, which I hadn't encountered before - I could guess what was going on, but it wasn't clear why it worked.After playing around I'm assuming it is a special case of switching on an enum, which also wasn't explained anywhere in the docs.
So, I explain how switching on enums works in the enums section, and then explain how captures layer onto that in the tagged structs section, explicitly pointing out its similarity to switching on the enum, instead of presenting it all at once without explaining what the enum branch syntax is about.
I considered doing two enum switches, one with the full
Volume.level
syntax in the switch branches, and one with the shorthand, but left that explained in the text. I do think it would make it even clearer, but am not sure it's worth the added verbosity.Also, I'm making assumptions about how things work here, I don't actually know how Zig works, so let me know if I've misunderstood!