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

Chapter 1: Explicitly explain switching on enums and tagged structs #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cincodenada
Copy link
Contributor

@cincodenada cincodenada commented Sep 4, 2021

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!

@cincodenada cincodenada changed the title Explicitly explain switching on enums and tagged structs Chapter 1: Explicitly explain switching on enums and tagged structs Sep 4, 2021
@cincodenada
Copy link
Contributor Author

cincodenada commented Sep 6, 2021

After running into the shorthand syntax again in #115 I came back and explained it earlier here. These changes will conflict with #115, but I think this is a better way to introduce it I think, so I think we should merge this PR and then I can update #115 to make sense with the new changes.

Previously, the first place we encountered switching on enums was in the context of tagged structs, where there were two layers of complexity involved. Instead, 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.
This is nicer, I think! The shorthand is a general pattern, so it's nice to introduce it with switches and then reference it in the method calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant