C++ Strong Typed Duck

Not a Duck. Amazing how similar. Obviously we need Duck Strong Type.

 

I admit. C++ Strongly-Typed Duck is a clickbait title. First a confession. This “strong type ” implementation is not my idea. Please make a slight detour into the direction of “Strong Type by Struct“.

Here we will try and formalize this idea in the realm of real-life C and C++ and intricate but significant differences between the two.

(the next episode of the “strong typing” mini serial is here)

This idea is basically yet another of those: “Just make everything to be a class”. Primordial C++  primordial idea. Rooted in SmallTalk. But, I don’t care if it is. It works. If used but not overused. There is no complex C or C++ behind it. Almost nothing.  Just simple guidance:

Make every “strong type” as one struct, with a single member named ‘v’. Name of the struct is the strong type name. Type of the v is defined by you.

Let the code speak. From the start, we need to take care of C vs C++ differences. And that is the bush we need to beat. Not around it.

That is my minuscule macro, that works for me.

Now let us “jump into the deep”, to avoid tiring you with the prose, instead of delighting you with a code. The use case is “Flying Duck Institute” IT support.  There will be measurements so let us do some physical units first. We do not want to mix kilograms and millimetres. So let us make them strong types.

And now we have some duck specimens to measure. We go further and use the same ridiculously simple macro to produce clean code.

We shall dive into C vs C++ significance shortly. For now, let us serve the “Flying Duck Institute” IT support. Look, here is the first duck flying in! Let us take care of recording the results of its measurement.

But. Wait a minute, that definition is a mistake! Obviously, height and weight are mixed up. Frankly that “declare and initialize” style has made it possible too.  So let’s do it nice and simple:

Huh? Is this a joke? That guy better takes some rest. He made a mistake again.  But. In C that mistake simply can not happen. That is because  C has “compound literals” and C requires compound literals to assign structure instances to structure instances. 

That C code is plenty of opportunities to remind oneself what is oneself doing and not make a mistake in that realization.

This does not mean you need to drop C++ and jump onto C.  After all you might find yourself very adverse to that C code.  Just please be aware of the C++ issue with anonymous field designators. Let us proceed. Duffy is immutable, but it has gained in weight

There are no guarantees here, one can indeed mistake height for weight when giving them values, especially in C++.

Why would anybody do this?

The key point is: we have described a Duck as a strong type. That is much (much) better than:

That is a simple code wide open to all sorts of bugs. And very difficult to maintain down the long timeline.  For the opposite presented here, one can say: “Strong Duck” typing indeed.

Lastly, if we imagine we are talking about flying rockets, not ducks, we might start to realize the benefits of coding idioms which are making mistakes hard to make.