Opaque Result Type
SwiftUI was presented, the new framework for building valid user interfaces for all devices.
One of the things that struck me was that in all the examples when building a View property, its body returned a type View to which the reserved word preceded it some.
In this article, you will see what it is some keyword and what it is used for, but I already tell you that everything has to do with Opaque Return Types.
Why we need Opaque Return Types?
Let’s start with an example Let consider that you are the (owner) of a mobile shop, a customer asking to show mobile. The shopkeeper shows you different companies mobile.
Protocol Associated type which works same they allowed to return the different concrete type we can’t predict which one.
If you select a particular mobile from rack, could have any of the following concrete types:
So if you had a function for picking a random mobile from a rack.it would need to have a mobile protocol as its return type.
Note: This would not be compiled.
Protocol ‘Mobile’ can only be used as a generic constraint because it has Self or associated type requirements
What does it mean:
It means compiler will restrict you to perform the swapping, equal, compare operation on the returned concretes type. Because Compiler cannot infer the associated type from this definition and the return type would be incomplete.
What is that about the Opaque Return Types?
Suppose shopkeeper keep categories the all device by manufacturer, so whenever we select the device from the categories manufacturer. it will always return the same type.
So whenever we call the selectRandomMobile function it will always return the same concrete type.
Hence we can able to perform the swapping, equal, compare operation on the returned concretes type. and the compiler will not restrict you(an error is gone)
How to categories the concrete type?
some keyword does same what you want in code
Adding this keyword turns the return type into an opaque type which means that you and the compiler both know that this function will always return only one particular concrete type — you’ll just never know which one!. You may get a Nokia always but you’ll just never know which one!
It’s basically the possibility to hide the type that returns the function/property/ subscriber and offer instead a protocol.
But is it same as using Protocols ...🤔
No, it is not the same, basically because the Opaque Types preserve their identity, because the compiler knows what type it belongs to, while with the protocols the compiler knows that it can be any type.
Generally speaking, prototype types give you underlying types of value they store, and opeque type let you make stronger gurantees about those underlying types.
The opaque result type helps you return a specific generic type without specifying which type with the help of the word “some”.
Opaque return type:
- It’s reverse of protocol generic type.
- It always returns same concrete type.you and a compiler knows it.
- If we use an opaque result type instead, we enforce that the function will always return the same concrete type.
- Inside function, while performing the operation we know the generic type.
- It’s basically the possibility to hide the type that returns.