Month: February 2022

As Smart as a Smart Type – Practice (C++)

In theory, practice and theory are the same, in practice they are not. So, after having read how brilliant and smart smart-types are, it is now time to have a closer look at the compiler and figure out what C++ can offer.

After my last post, I found that Smart Types are also known as Refined (or refinement) Types. And here is a notable implementation for Scala.

Simple things first, if you need a type with a bunch of possible values, don’t use int & #defines, don’t use bool either (please), use enum, or, even better enum class.

Now that we’ve done with the trivialities, let’s proceed to something more challenging – numeric types. Ideally, we want some template code that wraps the numeric type and saves us the boredom of writing all the usual +, -, *, /, ==, !=, <… operators, while letting us define the rules of the existence of the represented type.

Continue reading “As Smart as a Smart Type – Practice (C++)”

As Smart as a Smart Type – Theory

Recently I listened to a “Happy Path Programming” podcast episode about Smart Types. And that inspired me for this double post. The first part (this one) is about what a smart type is and why you should employ smart types in your code. The second part (yet to come, hopefully soon) is about the troublesome way I implemented an arithmetic smart type template in C++.

Continue reading “As Smart as a Smart Type – Theory”