Foreign constructors
Simplifying complicated constructors.
Recently I found myself extending a game engine with font rendering support. Fonts have a lot of properties, so naturally I created a tidy structure to hold all parameters. Whenever drawing a piece of text: I’d simply pass this structure along.
A philosophy I follow, is one of programmer convenience. For example: I always specify default values; when you instantiate a class without any parameters, it is directly usable. Following from that, whenever you call an method or constructor - you should not have to specify not used parameters. This adds a practical issue: it requires all kinds of constructors. Here is my initial structure:
This is impractical and ambiguous, notice how halign
and valign
have the same type. Types have to be unique for parameter order invariant constructors (one could use Tiny Types to alleviate this). Quickly enough I removed the constructor madness and created instances the following way:
Unfortunately, such Font
instance could never be declared constant. After all, we are modifying it post-creation. Sourcing inspiration from the visitor pattern, I’d like to introduce the foreign constructor to solve this issue:
The foreign constructor allows instantiation with the following syntax:
It requires some ASCII-art []{} to get the C++ lambda syntax working, but it feels clean to me and gets the const-ness right. It is actually very similar to the ISO C99 syntax for label-based struct instantiation, but without the drawbacks (e.g., only works with build-in types, no default values and no constructors).
Enjoy foreign constructors.