164 points · by Tomte · 3y ago · 19 comments
I just started reading about liquid types and I'm loving the idea : https://news.ycombinator.com/item?id=37349276
Take the most simple example, suppose I have an enumerated type, all these HedgehogSizes are either TINY or NORMAL or HUGE. Awesome. But, in C++ a HedgehogSize can be 6. Um. What? Oh says C++ I could see that's just an integer, and 6 is an integer so that's fine... Right?
In C++ both these enum types have an "underlying type" which is just an integer. That's the type you're really getting for most purposes.
In terms of syntax specifically you can cast (in C++ specifically static cast) integers to an enumerated type. This makes mechanical sense to C++ because of the compatible "underlying type" but as a direct consequence you don't have the kind of type safety these exercises are relying on.
Notice that if I cast 1.234 to an integer, C++ says that's 1. It does know that cast doesn't mean "just change the type but keep the same bits" and yet, that's exactly what happens for enum.