Kotlin Context Receivers Made Easy!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Salam friends and welcome back to the channel in this video are going to explore what are context receivers this awesome new features by cotlin and how you can use them in add let's get started so context receivers is new experimental feature by cotlin it was added in I think 1.6 C 1.6 so make sure you have a c version above that and for that reason they are not directly available you have to add them as part of cing options you go to your grade all stuff here and here you can see we are using something called cutlan options here we can add something called free compile arcs and we shall add things to this and this is a simple list so we can add things to it we can do it like following and we will add the following X context receivers this is parameter for the compiler okay now we can sync our project awesome now you can use this feature whenever you are defining a function before that let's understand what do we mean by context now let's say that I have a function and I wanted to use some properties that are in certain scope okay for example let's say I'm having a function called toast for example let's say I want to do a toast all right then of course in order to do toast you need two things you need a string and you need a context right so you can do toast like following do make text here we need the context and the string now let's make for example the string as what we call the receiver this is an extension function of string what does it mean is that I can use string in this function right I can have the string as this which is the second and I can do show now the only problem here is that context right because this is second third parameter right previously we did something like context here instead of string then the string will be here and then we pass the data here right and then we can use the data like that and with this I can do toast directly with hello okay because I'm calling this to on this context this is we call the receiver here because here the activity there's context right but what if I want to do this string here instead okay and still I don't want to pass the context there what I can do is that I can add property of this function to make it callable in certain context so I want this function still with this applied to only string I want to call it only from Context what I can do I can define context this is the propability you get by activating that compile arcs and here you can give it what is the context you want to call it from here in this case I want to call it from the context context this is weird context context anyways and here while we have problem here what do you mean by this do you mean this string or this context you can copy this one make at context like that and this data we don't have any data it is this of the string right and boom now you can call this toast immediately from the hello it means I can do hello do toast what beauty now the magic isn't only that let's say you want to use the same function in another context we can also add context or make combined context in order to call this function for example I can only call this function when I am in a context and let's take another example I don't know an end for example okay so it means this wouldn't work right how you can make it work where you can simply do the width the width function is awesome for such a thing I can call it with five so this will be an INT here so I can call the toast here because it is in the context of context and an INT okay sorry for using two context at the same time so this is the actual example on how to provide the context directly here you'll see another example with the Box in Jetpack compos so here you can see we are passing that t which is the one as a context for this Lambda so it can use it here all right let's take another example okay so if you remember if you are using jetpack compos you know that there there is some modifiers that can apply only to certain scope for example the alignment all right let's say you have a surface and here in this surface right I want to use for example the Align modifier I can pass the modifier of course and here I want to use the Align here the Align if you press Alt Enter you'll see there is alignment for books instance for column scope and also for row scope if you use one of these it won't work so this would never work even if I pass anything here for example alignment do anything it wouldn't work center vertically Center horizontally it won't work okay but if I pass this into certain context which is the Box context for example here this function would work like you see it is working just unpassing the wrong stuff okay so here it can accept only alignment center for example this would work perfectly but if I remove it from here it wouldn't work why because this align only work in specific context or scope you can see but it is implemented differently H here you can see in this modifier are implementing it in the box code so you can call this function unless you are in the Box cope and guess what here in this box right to allow you to run to execute this lambdaa within the Box scope that's why you are able to call such a method they implemented this way they are not using context receivers here but they can do so like the following for example let's pretend I'm having interface for example and let's call it my box okay and here I want to apply a modifier let's call it my modifier and this modifier I want it to be applied only when you are in the context of my box right I don't have to put it inside my box I can put it outside and guess what I can use the context and guess what what is the context when you are whenever you are with my box here okay and here let's kind of return modifier actually you can return this the same modifier so we are acting as we are creating modifier now if you use this modifier here it won't work why because it's not in that scope there is no required context found like I'm not in the context of this my box how can I pass this box you can do the same trick let's make a composable function for example let's call it my box for example this is simple thing and here in the same context I'm going to put content and here I can use mybox Dot scope for example and I can call content for example like that and here of course it would require the actual implementation of the box but it doesn't matter this is just an example can delete it now what I'm saying is that we can call my box here right and instead of passing the content like that I can put this surface here this wouldn't work because this isn't composable let me just put composable part here now you can see it is working and my modifier also is working because I am in that scope okay that's the idea behind this scope so actually what does it mean to have such a thing now it means you can add context directly to function without including them into some kind of object to make them constraint to certain objects and this is really really cool in order to make our code more cleaner and more expressive that's the beauty of coton now you can immediately jump into function add the context immediately with this one use this context here inside the function as we are using the context here connectivity manager are not using the connectivity manager so that's the basic idea so this is basically it for this awesome featured by cotlin now you can add this tool to your toolbox to create expressive and cleaner code in cotlin this is it for this video thanks a lot for watching this video to the end don't forget to subscribe to the channel and always see you in the next videos
Info
Channel: Charfaoui Younes
Views: 737
Rating: undefined out of 5
Keywords: mobile development for beginners, mobile development roadmap, mobile development vs web development, Jetpack Compose, Android development, Permissions, App development, Tutorial., UI design, Android Studio, Kotlin, Mobile development, Best practices, testing, UI testing, unit testing, app quality assurance, Android app development, productivity tips, software engineering, best practices., test, private, mutation
Id: sax8cX2CMgo
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Wed Apr 10 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.