Everything you need to know about Kotlin 2.0 🟣

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a couple of days ago on a cotlin conference in Denmark jet brains has announced above 80 new features and improvements that comes with a new cotlin 2.0 release the language is growing pretty fast in this video I will quickly introduce you with some of the most important updates that you need to know about stay [Music] tuned one of the first things that caught my attention in this new cotlin release was an in uction of extensible data arguments which is represented through the new data AR keyword in cotlin yay we got a new keyword right but what does it mean what problem does it solve and how can it help us to make our code more concise well first let me start from explaining the current issue that we have let's take a lazy column composable for example it's a well-known UI function with a bunch of different parameters that are available for a customization such functions heavily utilize the use of a parameters with a default values to avoid a lot of overloads that are required to cover for all those cases but what if we decide to add a new parameter to this function we would have to add a new overload to support the backward compatibility new overloads also means that we need to duplicate all the documentation for all existing parameters which introduces a lot of repeating stuff if you take the closer look at the function definition of a lazy column you will notice that those parameters are mostly used to add the various customization options and that's where the new extensible data arguments concept comes into play jetbrains wants to allow you to create a new class with a data R keyword to mark that class as a holder of all those customization arguments that you may have this can result in an easier maintenance and functionality extension of your existing apis they want to leave a door open for a library authors to have a full control over representation of their arguments by skipping an explicit creation of such classes at a call site which means when you call a lazy column with this a new argument type you don't have to initialize an additional class instead you can pass the data AR properties separately like we did so far the next great Improvement in a cotlin 2.0 is called guarded Condition it's used to help us check for multiple conditions inside the when block without repeating the state or the variable name so far if you wanted to check for a multiple conditions in a when block you would have to repeat the variable name for each separate case but now with the guarded conditions we don't have to repeat it anymore the new feature is going to get introduced with a cotlin 2.1 beta version and with that in mind we can use an if keyword to specify an additional condition all without repeating the variable name which is great the previous example that I have shown you can be improved further more with a context sensitive resolution that's another upgrade that we have received from a cing 2.0 we can handle instances of a seal types or enams directly without repeating the Base Class name when expected type is a well known this makes our code even more shorter than before the next update is about cotlin destructing declarations so previously we could destruct an object of a data class in a multiple variables where the name of each variable can be anything you want but now with cotlin 2.0 you can no longer write the variable names that are different from the actual property names of that same data class object that you are trying to distract with a cotl 2.0 if you try to write custom variable names here instead of the actual property names then you're going to get a compile time error however I have heard that they are also planning to add some kind of a special syntax that will allow you to assign new names to destructing properties but we'll see how that goes jetbrains also plan on removing component functions for a named based destructuring completely the next interesting update that you may have already heard about is an introduction of explicit backing Fields the primary purpose of creating two variables one mutable and private and the second one immutable and public is to be able to modify the mutable one only from within the scope of a single class without exposing its mutability outside of the view model for example that approach has a few benefits like encapsulation immutability and a controlled modification by using backing Fields you can encapsulate the internal state of an object which allows you to control how the state is modified and accessed exposing an immutable public property while keeping a im mutable private property ensures that the external code cannot modify the property directly which is maintaining a level of immutability from the outside perspective and controlled modification means that it allows the class to manage and validate changes to the property internally which can be crucial for maintaining consistency and integrity of the object State this pattern however introduced the creation of two different variables and since we are generally creating and maintaining a lot of different states in our project this introduced a lot of plate code as well luckily with this new update in a cing 2.0 we can declare a single variable instead and cut the boiler plate for almost 50% which is amazing the new solution provides an explicit field declaration where we can hold both mutable and immutable State mutable state is private and available only within the scope of the parent class next there is also an update related to string literals and string templates previously if you have used a triple quoted string in cotlin you couldn't get the reference of a certain variables or Properties by using a dollar symbol it didn't work however with a cotlin 2.0 we can do exactly that with this special syntax by adding a $2 symbols before the starting triple quoted string on the top simple and easy the next update in a cing 2.0 is related to Union types for errors Union types are a feature in some programming languages that allows a variable to hold values of a different types they are used to describe a value that could be one of a several types effectively providing a way to handle multiple types in a type safe manner however in this case cotlin doesn't introduce Union types in general only for errors in your code as far as I understand its main benefit should be for unchecked casts that we have seen so far in one of those functions like this one with error Union types we should mitigate that issue of unchecked cast in our code with this example right here you can see that we have created an error object that will help the compiler understand the type in our code and automatically cast that to an appr rate value while we are here talking about smart C from a cing 2.0 if you declare a variable before using it in your if when or while condition then any information collected by the compiler about the variable is accessible in the condition statement and it's a block for a smart casting this can be useful when you want to do things like extract the Boolean condition into a variable then you can give the variable a meaningful name which makes your code a lot easier to read and easily reuse the variable later in your code for example in this code right here we will be able to access an information about the cat class we can also produce a multiple smart casts within a single variable in this case the first one is from a type check and the second one is from this is null or empty function both of them will be applied to an appropriate block resulting in a smart cast smart cast after a logical operator or are now possible too for example let's say that we have a one parent interface and a three child or sub interfaces previously the compiler didn't perform any smart C after a logical operator or so if you check if the variable is either one of those two we still couldn't invoke the signal function and with this new cing compiler we are now able to infer that this variable is the closest Super type of the postpone or decline types which is the status therefore we can invoke the signal function without any issues pretty neat the next Improvement I want to mention here is about making your cting constructs even simpler and safer than before like for example we can now create and combine both long and integer ER types with this Plus in equal operator without any compile time errors because compiler can now dgar this code and then perform all necessary conversions by itself even with nullable types cing compiler can now dgar this code and make safe operations before executing this code which in the end makes your code more cleaner so those were some of the most notable updates for me so far and there are a lot of more actually so if you wish to check them in more details I will leave the link down in the video description anyhow what's your thoughts on these new updates do you find them helpful do you think that they will increase your productivity even more comment down below and let me know other than that don't forget to leave a like but only if you find this video helpful thank you for watching God if I die I'm a legend when they lay me down to rest
Info
Channel: Stevdza-San
Views: 58,182
Rating: undefined out of 5
Keywords:
Id: EuBXe-ZZMWE
Channel Id: undefined
Length: 11min 5sec (665 seconds)
Published: Tue May 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.