|
|
|
|
|
by lynx23
590 days ago
|
|
I am not an active Zig guy, so... YYMV, but... To me, the examples are actually not self explanatory. What does the _ do? How are these constants used? Why is it something special? C had enums since forever, what is the novelty here? |
|
The basic idea is the "newtype" pattern: different types may have exactly the same valid set of values, but not be interchangeable.
E.g. kilograms and US customary pounds, or meters and miles. If you've got a function `f(distance: double)` where `distance` needs to be in kilometers but your program also handles miles, it's nice to be able to define a type for kilometers that's different from the type for miles so the function won't compile if passed in miles incorrectly. So you get `f(distance: kilometers)`. That also stops someone passing in pounds to `f`, or any other such nonsense.