Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nope, this is not relying on optimization, it's just how compile time evaluation works. The language guarantees "folding" here regardless of optimization level in use. The inline keyword used in the original post is not an optimization hint, it does a specific thing. It forces the switch prong to be evaluated for all possible values. This makes the value comptime, which makes it possible to have a comptime unreachable prong when switching on it.

There are similarities here to C++ if constexpr and static_assert, if those are familiar to you.



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

Search: