Opaque Type

 

If you’re new to Swift, the term “opaque type” might sound confusing or intimidating. But fear not! Opaque types are a really useful tool that can help you write cleaner, more flexible code.

So, what is an opaque type in Swift? Simply put, it’s a way to hide the implementation details of a type while still exposing a public interface for it. This can be especially handy when you’re dealing with complex types that you don’t want to expose to other parts of your code.

I know that it sounds very unclear and confusing. Let’s see an example that will make it easier to understand.

In this example we created a protocol that has an id, this id can be String or Int depending on the Car mark. To be able to do this we used associatedtype. But the use of associatedtype created for us a problem, now we can’t create func and add the return type of our Car protocol. Protocol that has associatedtype can’t be used like a return value like a regular protocol. If we will try to do it we will see:
  • This warning in oldest version of Xcode 
  • Or this in newer

But what does it mean???

The problem is that our func trying to return the Car protocol, and XCode Compiler wants to know in advance to which type associatedtype was set. And this ruins all our plans to make more useful and generic code. So we must find a way to provide this info to the compiler. And this is where the keyword “some” helps us. 

The keyword “some” says to the compiler that the type of car that will be returned will be with a specific type of the associatedtype, but we don’t want to expose the type to the caller of the function.

And now you will be able to sign to return a Car Protocol without specifying which car exactly you return and which type you signed to the associatedtype. 

For the next example, we will look at SwiftUI, because there we can see the keyword “some” and opaque type everywhere. For example, the automatically generated code that we see when creating a new swiftUI project.

Here we can see two structs that each have computed property of type “some View”. But why do we need it here??? The answer is very easy. Let’s see several examples of how we can do it without the “Opaque type”.

In this example, the body type is text(instead of some View), and this code will work fine.

And this Example is exactly the one above just instead of some View, we show exactly which type we return. And it looks very confusing and difficult to understand. To avoid this we use the Opaque type with the keyword “some”. We say that this variable will return some view, it can be only TextView or a more complex view with several views in it, now it no matter how big and complex the view in it, the compiler will know what are the actual return type and the one who uses it will see that he receive a View. 

Overall, opaque types are a great tool for writing flexible, modular code in Swift. By hiding implementation details behind a public interface, you can make your code easier to understand, maintain, and reuse.

Author: Arthur Zakharov.

Leave a Reply