Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Newtype Index Pattern in Zig (matklad.github.io)
4 points by birdculture 7 days ago | hide | past | favorite | 4 comments




Nice Article. However I fail to understand how using the enum prevents the usage of the wrong index in a data structure. Can you explain further?

I guess you would need to cast the index to the enum or you get a type error, which should narrow down the possible error locations.

Thanks for your response. Wouldn't using the `_` in the enums somehow make almost all integers index "cartable" to the enums?

Yes, but you have to cast it explicitly. If you have two different arrays each with its own enum, the compiler would throw an error if you used the wrong one.

Sorry for the late response and happy new year!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: