Compiler time safety with phantom types

Swift, unlike Objective-C, tries to be a safe language. And by safe language, I mean that tries to force you to rely on compile-time safety checks, enforcing the notion that code should be as safe as possible at runtime.

So, in order to do that, Swift enforces a very rigid, even pedantic type system. Which can be certainly annoying sometimes, but, on the other hand, it can help catch potential errors at build time, by being smart and relying on the compiler.

Phantom types are types that are only declared in order to increase the compile-time safety of code.

For example. Imagine we need to write a function that assigns dishes to a meal. We can pass the meal identifier and the dish identifier to that function, and it will match both.

But if we pass those parameters in the wrong order by mistake, we won’t have any error at build time, although the code will not work as expected.

However, let’s say that we declare a Generic Identifier type.

Also, we declare two phantom types, in this case as enums (so they can not be instantiated). We want these types only as “tags” we are never going to actually use or assign them.

So now, we can rewrite the previous function to:

We declare the new Identifiers, tagged with he phantom types:

And now, the compiler will complain if we mess up:

Leave a Reply

Your email address will not be published. Required fields are marked *