Zig NEWS

Discussion on: Using comptime to invert bijective functions on enums

Collapse
 
slavka profile image
slavka

Oh, I see, so the value between the | signs in the inline else construct is associated with the "current iteration over all possible enum values", which actually makes sense. Thanks!

I suppose the article could benefit from some version of this explanation, since it highlights a less obvious point about this feature of the language, especially if the reader hasn't had an opportunity to use it.

By the way, is the last snippet in your comment written by hand? If not, it would be cool to see how it was generated, otherwise it might be a good idea to include the generated machine code or something like that to verify that there's no actual work done at runtime.

Thread Thread
 
rbino profile image
Riccardo Binetti

It's written by hand to give the intuition of how that works, but if you look at the output of the code on GodBolt and search into the assembly, you can see that the beats functions is never called, and the beatenBy functions is translated to a number of comparisons, jumps and movs of constant (the winners calculated at compile time).