Swift Opaque return types (some keyword) and protocol associated types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey there welcome to another episode of coding with code card, this is Ravi and in today's session we are going to understand the some keyword which is also known as the opaque type So in order to understand the some keyword, let us begin with an example Let us assume that we have a manufacturing company and this company makes two products one is a laptop and the other is a keyboard and if I want to present this in a code this is how it is going to look like so I will have a protocol and let us name this protocol as product and This is going to have a function Name the description and This description is going to print product description if it is a laptop we'll say this is a laptop and if it is a keyboard this will say this is a keyboard so I have a structure named laptop Which is going to inherit from the protocol product And as I said, this is a laptop and Similarly, we have another structure. I will copy this out and I would say this is a structure keyboard And this is keyboard Every product has a product code now Sometimes you must have observed product code can be either a string and sometimes few products product code are an integer So what we are going to do in this case is laptop will have a product code of string and the keyboard will have a product code of an integer Since both laptop and keyboard are inheriting from product It makes more sense to declare the product code property inside the protocol but here you have a unique requirement the requirement is the property we have here, it's going to be unique i.e. in case of laptop it's going to be string and in case of keyboard it's going to be an integer so in this case, what we are going to do is we are going to use Associated type and what I'm going to do is I'm going to say code and I'm going to declare the property product code of the type code and then I would say get and now when I implement this I in the case of laptop we decided the code will be a string and I would say Product code is of the type string and in the case of keyboard the code is going to be of the type integer and I would say product code is of the type integer so far so good and I think this is the first time that I'm explaining associated type in my channel and right now, you know What is the use case of using the Associated type in your project? So right now we have everything. The only thing that is pending is manufacturing So what we are going to do is we are going to say we have a factory where these two products are going to be manufactured so I will have a function I would say make products and This is going to return me product Now over here Swift compiler is complaining and let us see what it has to say Protocol product can only be used as a generic constraint because it has self or associated type requirement I am not aware of self in this case Associated type is something which we have used as you can see over here We have used associated type because of our requirement in case of laptop we wanted the product code to be string and in case of keyboard we wanted it to be an integer And that is it That is the reason why we have used associated tyes, the compiler is expecting us to return a concrete type now The problem is because of this associated type we have this error and removing the associated type will resolve the error because the error clearly mentions it has self or Associated type requirement and this is the reason why swift compiler is getting confused as to what are you going to return? product protocol has two different implementations here because of associated type and that is the reason why swift compiler is not liking it so in order to explain the swift compiler that we are going to return just one concrete type have to use the some keyword and Right. Now what you can do is you can return you would say laptop and and the product code can be let's say test Now the swift compiler is not giving us any error because it knows that you are just returning one concrete type Let us say this was just for demo purpose to make you explain like, how does the some keyword work With a protocol now, let us say if this were a real project I would not have coded like this. What I would have done is I think you already know is I would have directly written laptop and In the case of keyboard, I would have directly returned keyboard over here would have done something like this The reason is this is much simpler But this is not the actual use of the some keyboard the actual use of the some key word is encapsulation That is hiding your concrete type from the real world. So in order to explain that let me show you an example So let us say I have a function and let me call this function as square array elements and This is going to accept a generic parameter, which is going to be T of numeric and I would say array And this is array of the type T so far so good return type of the function would be, let me type it first. So this is going to Return you a lazy map sequence and this is going to be array of T Comma T and implementation of it is actually very simple This is going to be array Dot I would say not my app I would say lazy oops lazy dot map and I would say Where is the dollar sign I would say dollar zero x dollar zero Okay. So in this case the concrete type is lazy map sequence now, let me show you the summary of this This is a structure which is inheriting from the sequence protocol and lets say if we have a type inheriting from a protocol It is sequence. Then what you can do is rather than returning something like this. You can return something which is more elegant That is I would say this is some sequence so I am now returning a protocol This lazy map sequence was inheriting from a particular protocol. And in this case it was the sequence protocol so what i have done here is So in this case, you can see key how the some keyword is helpful we have not returned the lazyMapSequence but What we have did is we have just returned a sequence I think that the some keyword is more suited in situations like this if you are working on a utility or a framework project then in that case what you want to do is you don't want to return the concrete type rather than that what you want to do is you want to return the protocol and For only that reason I would be using the some keyword in live projects, I don't think so there are any uses cases for the some keyword as you can see you can code like this and return a concrete type as well Now there is one more place, where opaque types are used extensively So let me switch to a swiftUI project at line no 12 you can see the variable body which is of the type some View at line number 18 you have a Static variable which is previous of the type some View and right now we have learned that some keyword should be used with protocol. So let us see what this view is so what I'm going to do is I am going to jump to definition and over here you can see this is a public protocol or and the code is pretty much similar to the demo we did today So let me switch back to our to our example Now let us say you did use the some keyword in your project. I am really excited to know technical reason behind the usage How it has helped you so do mention that as a comment or you can email me My email id is codecat15@gmail.com so i hope after watching this video you must have got an idea when to use Opaque types the code done in today's session is available in GitHub and Google Drive and the download link is mentioned in the video description so if you need the code, what you can do is you can check the video description Click on the link and download the code for you Now one request is if you're learning something from my videos and if it if they are helping you in improving your code then please Do support the channel by subscribing it and sharing it with your iOS friends Now that being said, thank you so much for watching this. Have a nice day and happy I coding. Bye. Bye
Info
Channel: Code Cat
Views: 14,127
Rating: undefined out of 5
Keywords: opaque return type swift, associated types in swift, some keyword swift, protocol associated type, what is some keyword in swift, some keyword swift hindi, some protocol swift, swift programming tutorial hindi, associated type swift, swift associated type, swift some return type, what is opaque in swift, what is opaque in swift hindi, swift hindi tutorial
Id: Wk4UzzePfb8
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.