Kotlin for Java Programmers by Venkat Subramaniam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Oh, having your language being talked about by Venkat is a huge milestone. Congratulations :)

👍︎︎ 6 👤︎︎ u/JavaSuck 📅︎︎ Nov 12 2017 🗫︎ replies
Captions
[Music] alright let's get started welcome to the session on coupling for Java programmers my name is Venkat Subramanyam well talk bit about Cartland we'll look at some examples see some of the motivations for using the language so what it appeared to me was that they took Java and they decided to add all the good features you could find into the java language and when they got it done they just called it Kotlin and so Catalan is actually a pretty powerful language and as I started looking at Kotlin and playing with it time and again I cart got reminded about various features that are really powerful and things I like in other languages I program in about twelve different languages and so I'm naturally drawn towards languages key strengths and features and I can relate to a lot of different features in different languages that really shine well in the case of Cartland well Kotlin is a statically typed language which which really is saying to us that we can get a lot of quick compile time feedback but the Carton compiler also is pretty powerful in terms of the feedback it provides as we are writing code and of course Cartland compiles down to java that should be no surprise as in java bytecode but it also compiles down to JavaScript if you really want to look at javascript one of the things I really like about Kotlin is that it's really fluent it's elegant it feels right when you write the code in terms of the syntax and the way that it cohesively comes together it's it's elegant like I said it brings one of the things that really excites me is it really brings wonderful features from a lot of different languages like I said I programming about 12 different languages and often time I'll enjoy a certain feature in a language but it wouldn't have other features which I liked from other languages it's kind of like a dream come true in a way you can think about carton like a trait where you bring a lot of different things together and then it becomes a melting pot for you to be able to use that so let's talk about how we can actually use this in the language well as you can see I don't use any slides but if you want to download the code examples I'm going to show you here you're most welcome to download them from my website on the Downloads link you can download it anytime you want to so let's talk about compiling Cartland and playing with it and what I'm gonna do here is start with a fairly simple example I'm gonna just edit a file called Katie Katie being the extension for Cartland so I'm gonna just start with a little file called sample Katie and and I'm not gonna put any ceremony here but I'm gonna compile it so a little bit of ceremony is required so I'm gonna start with a main function as would be traditional in Java applications I'm gonna say this is args over here and I'm gonna create an array of string let's say and in this case all I'm gonna do is simply print line let's go ahead and say hello but let's make use of the arguments that is given to us so I'll say Arg square brackets 0 so that's the little code I'm gonna write well you probably noticed already one wonderful feature here or the lack thereof I don't have to put semicolon I can't tell you how happy my little pinkie is already so I don't have to put that stupid semicolon every time I turn so I've written that little part but how do we compile this code and how do I use it well that's where the car-cling compiler comes in and there are a couple of different ways to do things so here is a cartoon C which is the cartoon compiler I'm gonna compile this file called sample Katie well one of the things you can actually do which I really like is you can include the runtime by including the runtime you don't have to really provide the classpath to Cartland it gets bundled into it which really reduces the burden on us to deploy this and use it so all I'm gonna do here is just simply compile it and produce the sample jar file and once I do I can run this like Java generally I can say clients path right here and then say sampled our jar and I'll provide the name of the file in this case this is going to be sample dart sample Katie and then let's go ahead and say world here because we want to say hello world to begin with so that's our Carling program running through java that was fairly easy well if you don't want to do this you can also do one more thing which is to simply compile cortland without the runtime and then when you run it through java you would have to specific where Catalan is located or simply you can use Cartland without the C and then just run it like that as well like you would in other languages other than Java so those are a couple of different options but one of my favorites really when it comes to languages is I really want to play with languages as directly as possible and so I'm a huge fan of repple and Carlene also comes up with a fairly decent repple that you can use I'm gonna get rid of the files I just created so at this point all I'm gonna do is simply say Cartland in this case just see and that becomes the repple that we can start right off the bat so I can just run the code right here and experiment with that and so that is right there Carlene telling us print line hello and are high and we can play with it just like that but again I'm a person of low ceremony I want to do the minimum I can and start interacting with the language so I'm a really big fan of writing scripts where possible so I'm going to create a file called sample kts where the S stands for it's a script and not a full-blown class we're gonna write so I want to really create a script and play with it right here so as you can see in this case we're gonna simply go ahead and type that code and say you know hello there without any other ceremonies we can just go ahead and run this so what I'm gonna do here is simply say a Cartland C again this time but I'll type in a scrip and provide the script file so that we can't directly run it as a script as well so you don't have to really compile and play with all of those things so there are some of the beautiful options you want to compile it be traditional to create a bytecode and then put it away you can do that you can play with the repple if you want to or you can just write it as a script and play it as a script as well that becomes really easy so for most part that's what I'm gonna do here just run it as a script and play with it as we go along so now that we have done that let's talk a little bit about some of the real nice interesting features what I mentioned already that semicolon is optional so you don't have to really use it one of the things that I really like about Cortland is a sensible warnings so if you are trying to use a variable but you haven't really used it properly or if an initialize the variable properly you'll constantly get some warnings which can be really you know useful to prevent errors from the get-go but like I mentioned cotton is a statically typed language but one of the things that it's important to keep in mind is when it comes to the word static typing I used to be really angry about static typing in the past and the reason is usually static typing meant you type a lot with your fingers that was not a really good thing to do well when good type inference you the language do the inference for you rather than you having to really type all the detail so in this case I'm gonna say let's say greet over here and let's say this is our type string and then I'm going to say hello right there and I want to just go ahead and bring the greet variable value right now let's quickly take a look at what we just did well this is an example of how you define types in Cartland now you probably really look at this and say gosh this is like Scala or you could say this is like typescript this is like well many different languages actually do this and this is quite different from what we do in Java in Java you normally say that type over here for example if you're doing Java you would say a string and then greet well languages like Cartland emphasize that the name of a variable is more important than the type of the variable so as a result they put the name first and the type after would that kind of emphasis so as a result in here you specify the name of the variable followed by the type this is how you specify the type anytime you specify it but the good news is the type is actually optional you don't have to really provide the type make no mistake this is not dynamic typing it knows what the type is at compile time and it knows you deal with it properly so if you were to call a method on this you'll get an error saying string does not have it so if you want to take a look at what class we are dealing with we can quickly query this and find out it's none other than the Java dot length dot string that we are actually using in this case so that's an example of type inference my recommendation is make use of type different inference as much as you can and and of course if you're writing a public-facing interface it's a good idea to specify type information if you're writing a local variable for example I would say just don't bother saying the type let the in front kick in and we can use that fairly nicely but then going forward you already saw me use the word called valve a minute ago let's explore that for a minute if I said Val let's say you know let's say number is equal to 4 and I want to print the value of number I can do that readily with a - friends kicking in right there but on the other hand if I said number is equal to just four again you will notice I get a compilation error because Val means it's immutable I cannot assign it to the left-hand side of an expression or assignment so as a result I cannot modify the variable it's an immutable variable but having said that Catalan also has yet another keyword and this keyword is called var which is also known as the key word of shame so if you're gonna use this keyword you have to hang your head low and don't make eye contact with the fellow developers so the point really is we're creating a mutable variable which is okay in a limited situation but not too broadly but this really is a way to really examine the code and say where am I using Val compared to var maybe and I can find out very quickly but in this case as you can see I can modify the variables and then of course if I modify the variable I get a new value assigned to it that's perfectly fine but on the other hand what happens if I try to put a string into this well of course in this case you can see I get an error type mismatch because this was inferred to be an integer of course and as a result you cannot assign some other type to it so that type safety still kicks in fairly well as we can see one of the things you can do is a nice little string manipulations and string templates if you used to grow we the syntax here is pretty much very similar to groovy in this context so as you can see some of these are kind of like in language like typescript and Scala then there are some features which are kind of like in groovy so in the case of a multi-line string there are quite a few interesting things you can do for example suppose I had a name let's say is equal to let's say Bob over here and I want to really print out hello Bob I could say hello for example and then simply put the name right here and as a result you can see that it says hello Bob you don't have to have the ceremony of the curly braces but I would encourage you to put that in if you have some other expressions then of course you want to put a Curly's around it to say what is the boundary of that expression itself on the same note you can also define water called multi-line strings you don't have to the pain of saying plus plus plus and keep adding things so you can very readily create multiline strings without really putting too much effort into it so to create a multi-line string very simply what you can do is it's it's really fairly low ceremony for example you can say message is equal to and just put three triple quotes right here and then start writing whatever string you want to say for example this is a message and we'll just put a little next line over here and say let's say written on and then we could say and sent it to well you could also have in this case variable names are as well and then you can put more stuff into it so in this case of course I have a little name as a variable so we'll go ahead and say name equal to Bob again well if we go ahead and print the message you can see that the message is going to be the string we created but of course we got little indentation going for us what if I don't want that indentation well if you don't want the intonation you can put a little vertical bar this feature is very similar to what Scala provides you put a vertical bar and then you can come down here and say you can say trim margin and then you can simply ask it to trim the margin that you have in there but you say wait a minute what if I don't want the margin to be a vertical bar well you can customize that by providing the character you're gonna use for the vertical bar so in this case of course you can see you can provide an argument but if you really wanted to you could also provide a different argument if that is the you know symbol you're using to indicate that it's the start of this obviously in this case you would go back and use the till D to indicate that that's actually the one that you're using as a margin separator so that's an example of creating a multi-line string but one other thing that really catches my attention is when languages treat things in a nice little way well one of the things we have learned over time is think about the word expression versus the word statement I tried doing this say the word statement and see how grim it feels like life was taken away on the other hand say the word expression you immediately start smiling don't you it feels light well that's why I like expressions more than statements the reason I really don't like statements is by definition statements introduced mutability how evil they are so you cannot do anything with the statement other than costing mutability and the more statements we have in a language the more we force upon mutability honest but expressions on the other hand do not force mutability they actually returned results back to us so the more the merrier expressions that's better in a purely functional language there are no statements there are only expressions in a purely functional language but of course we are programming with a hybrid language so it's a mixture of statements and expressions we normally have together but when it comes to expressions the more we have is better like I said so here is an example let's say we have age equals to 17 now I can say I can vote equals I can say if age is greater than 17 I can simply say please odhh vote over here and then I can say else well we could say not so fast so you can see in this case I have a little expression but this is not a statement if becomes an expression as a result whatever the if is returning can be assigned to a variable very easily so in this case of course if I were to print out the value of can't vote you can see that in the says it's it's not so fast which is the value that comes from the else part right here similar to if I try and catch is also an expression so whatever you do as a last statement within a tripe block becomes an expression of the try itself so that becomes easier to remove the ceremony and also prevents unnecessary mutability and garbage variables in the code as well so in general the code becomes a little bit more concise the more expressions we have and Catalan is leading towards in the right direction in in that sense let's talk about functions though oh well functions how do we create functions well you know we all program about maybe 8 to 12 hours a day so cartoon wants us to remember to have some fun along the way that's why they call it fun so every time you want to create a function you call it fun well that's exactly what Lang gives us as well so you are normally used fun to define functions in these languages like Erlang and Cartland so what you do in this case is you say fun and then you could say greet over here and going to specify your name let's say it was string and then I'll specify your message let's say in this case also a string and then we will go ahead and just print out a result out of this well before we go through this let's put equals right now and we will say in this case message over here and then a dollar name well notice what I just did here I wrote a function called greet greet takes name and message as arguments I specify the type of the arguments but I put equals the equals is a fairly limited capability in Cartland but the equal says please do type inference for me and figure out what the return type of this function is like I said this is very limited capability so this one Lee works for very small function usually a single line function this works really well if it's a little bit more complicated you would have to do a little bit more work for this but in this case I can go ahead and call the greet method and I can say over here for example J in common let's go ahead and say hello and you can see that in this case we are calling the greet which is returning the hello Jane and we are printing the result out of it so this is an example of using type inference while we are writing a method itself but you can also specify the type if you really wanted to so you could say : string as you can see here and then you would put a curly right after that and then you can implement the method you want to implement right in here so for example in this case I'm gonna say a dollar message let's say and then dollar name and in this of course I'll call greet one more time and we will just pass through this function an object of Jane for the first parameter and let's say hello for the second one and this time we wrote it as a separate method but in this case it's returning the string isn't it so it tells us we are supposed to return some result from this right there the type checking kind of came and said you need to return some value so I'm going to put a return statement right now and then return that particular value from that particular call well in this case of course I'm going to go back and print the result at this time and you can see that it is going to print the result of that particular call but what if it want to write a void method I'm not interested in returning any result from it well in that case of course let's go ahead and move the print line again over here and of course for this one I'm gonna make this a void method if you want to really make it a void method you could type unit but I would just leave it out why bother about the ceremony and you can just write it as a void method just like that not bothering about the unit but unit is just the representation the void in Bangui like Scala and in Cartland so right there's a function we wrote pretty nicely well so we wrote a function we looked at the return type inference we talked about specifying the return type we talked about the returning a void method as well but here comes a charm I'm a big fan of default arguments I'm used to default arguments from languages like C++ and typescript and all the way to Scala well why do we care about default arguments people will tell you the fault arguments are really nice because you don't have to send an extra argument I don't buy that that's not the reason I like default arguments the reason I like default arguments is it's a nice way to evolve an API so if I already have a function with let's say two arguments but I realize I want to add a third argument by making it a default it becomes a nice way to transition existing code can still work at the same time I can add new properties and parameters to this function very easily that's one of the reasons I really like default arguments so in this case I'm gonna provide a default argument let's say in here I'm gonna call greet and pass Jerry to this but I'm not sending the second argument as you can see now when I run this code it will fail obviously because I need to pass an extra argument but I'm gonna give a default value to this so I'm gonna say equal to hi right there so we are providing a second argument to this function as you can see in this example so given the second argument I don't have to really pass an argument for the second argument it just simply takes a high but one of the other things you will notice in here is this is again a feature available in modern JavaScript in es6 you have default arguments but in es6 default arguments can refer to other arguments other parameters in your function Cartland provides that capability also so I can come in here for example and knowing that is already ahead of this I can say named art length for example over here and as a result when I run it it actually doesn't say hi Jerry it says hi 5 Jerry so I can do that as well by combining the steering is very happy as you can see so we can just put these arguments and use the parameters earlier in the parameter list as well so again this is a feature I've seen in JavaScript you can do this in Catalan as well pretty nicely so you can use default arguments can tie these do this but going a little forward one of the things that gets really hairy is when your arguments become in order to please large when you have multiple arguments it can become really painful so what you can do instead of working with it this way is you can simply call greed but you can say name is equal to in this case we'll say a sarah for a minute and then we can also specify the argument value message is equal to and they don't have to be in the same order obviously and I can say howdy for example and pass it and so you can start giving these values as named arguments as well but of course you can also mix them together if you really want to do so you can pretty much we write this as greed but you can then send Sarah right here and then you can also say name equals to you know hello for example and so you can also have positional arguments followed by of course you can also provide the values for the other arguments too and in this case of course we are sending the message so if you have multiple arguments you can choose to send some positionally as long as you keep the order and send others using the names also so it gives you all these combination of flexibilities some of you may recognize those capabilities from groovy as well so Cartland brings that really nicely in here with with the fairly good type checking so that's basically about named arguments and how you can use them in here and you can mix them together one of the other things of course is we really want to use nice a variable number of arguments so in this case I'm gonna create a max over here we'll call it as numbers if you will and this is going to be a type integer however I'm gonna save our args on this so for args me to pass multiple arguments to this particular function so in this case what am I going to do I'm not a leverage type inference for a minute and another thing I'm gonna do here is to simply say number start reduce and I'm gonna take a max value and the element given to me and I'm going to simply return if max is greater than an element simply return max otherwise return element for me so that becomes a nice little cute little function for getting the max and I'm using the reduced function here to get the data out of this and return to it well this is a lambda expression so I'm gonna put a little curly on this so lambdas can appear as a curly outside or within a parenthesis if you want to put it but the curly is always required in Cartland so if you're gonna write a lambda it always requires the curly so you could wrap another parenthesis around this which is kind of wasteful so what I can do now is I can call the max function oh maybe let's with 1 and 2 to get us the result of 2 in this particular case but I can also call print line max this time and I can say 8 let's say 7 3 9 and 4 and I can get that value out also as a value of 9 but what if I really have an array already with me so let's say in this case I have values equals let's say int array off and in this case let's say 4 5 and you know let's say 20 3 & 2 well I want to pass this over to max and and get the response for these values but its course in this case if I say values right here what's going to happen in this case is we get an error type mismatch because it cannot pass through this obviously an int array when of our Arg is expected so what am I going to do to fix this well the beauty of this is the spread operator which we can use very nicely or some languages call it as explode operator well in fact we can just not only use that we can combine it with other operations also so in this case I say 1 2 3 for example and then I'm going to explore our spread values and then I'm gonna say let's say again I know 9 + & 2 well now of course when I run it the values contains the biggest value in this collection so as a result it brings back 23 so I can combine these together so you can have a combination of discrete values and you can spread or explode you were value also and then combine things together so that becomes really nice to start programming we're not spending over time on ceremonies it just becomes natural once we learn some of these syntax available to us all right so let's talk a bit about the traditional four loops but that again becomes a little bit easier to work with in the language how about writing a little for loop to play with this so I'm gonna say 4x is going to come from 1 to 10 and then in this case I'm gonna simply print the value of x keep in mind in this case it includes the value x over here this is something I've learned in Ruby and I really like this syntax but unfortunately in Ruby if you want to include a value and don't want include a value you put two dots and three dots and that is called cruelty right because a programmer is gonna like look at this and say is the two dots or three darts and as I'm getting older that's not really helping me well so Carlin said you know what we're gonna just use the two darts to give you a range but if you really don't want to include the value we'll just make it a little easy for you so we're gonna say it for you know X or in one until ten well that doesn't include the value 10 as you can see so if you don't want to include if it's not inclusive then use until if it's inclusive then you can use the two darts to get to it what about stepping through some values in the collection well if we want to step through the values in the collection you can say step two for example and then of course you can just skip some values and go through it you can also do a step down if you want to do so in this case for example you can say 10 and then you could say for instance down to oh let's say 1 and we can go from 10 all the way down to 1 and we can do that and of course you're thinking what if I really want to skip values doing it now I say step and obviously the confusion is is a 2r minus 2 well okay it's actually 2 if you do a minus you can accomplish an error so you can skip values as you drop down as well you're just positively decrementing in that direction of decrementing so that's an example of very nice elegant loops as well that you could use of course you can also iterate over a values also very easily so for example let's say we have a collection of names on our hand so I'm gonna say Val names is equal to let's go ahead and say in this case list off and we'll call it as Tom let's say and a couple of different values over Jerry and one more maybe we'll just include a spike so I want to iterate through these values so we can simply say for name in names and then we can start iterating through the name and print the value out very easily as we go through this well that's great but what if I really want the index value given to us in this in this case well we can also get the index value can say index and then we can say named art indices and then we can get the value the index value through it so we can start putting counts on it as well but of course in this case because we have these string expressions we can use we can take the index value and then if we chose to we can also say names square bracket let's say the index and get the value art of the collection as well in this case that get of course so we can get the value of the index also fairly easily and process that if we really want to do so again very elegant and cohesive and fluent syntax for four so whether you choose a functional style of programming or whether you choose the imperative style of programming you got it covered so you can enjoy the fluency in both cases fairly well and and and that's what Carleen does I do have to mention that Kotlin does have a few rough edges there are a few times I would get annoyed with it because when I'm trying to write the code if I deviate slightly from the syntax I would not get an error but it would give me a in return a lambda expression at times which is not what quite I was expecting so there are a few rough edges and hopefully over time that will get really a streamlined and and taken care so but one thing I really enjoy so one of the things I I do is I'm a big fan of beauty in languages that's what I spend my nights on there playing with different beautiful things on languages and and so I was right over very clearly about maybe a two years ago I had in front of me a ruby code and they say Ruby is elegant and beautiful too to critics and readers but as looking at the Ruby code and in front of me was an if statement which was about ten lines of code and and you never can go to bed when you have an ugly code in front of you and I sat there and I was taught at googling started reading started searching and then I came to I said to myself there's got to be a better way to do this right and then eventually I stumbled on this beautiful class and Ruby called the when class and I just dearly loved it and I was so thrilled to see when actually in Kotlin as well so when is actually a very beautiful pattern matching syntax it can remove a lot of ceremony in your code and I think I've done it fairly elegantly here as well let's take a look at one example here so I want to create a function called process but it's going to take an input I don't know what the type of the input is so you can say the type is any in this case and then I'm going to say over here when and I'm going to put the input on it and put a wind block around it now I'm gonna go ahead and call this process over here and I'm going to when I call the process I'm gonna send a value of one so I'm gonna put a 1 right here and I'm gonna simply print you a got 1 and in this example as you can see when I call the process it simply said you got 1 so I can certainly do that I can call so called process and I can send a 7 I can call process and I can send let's say a 8 but I want to know that if it's one of these two values so I can come in here and say 7 comma 8 and then of course this says you got a 7 or 8 so we can start putting these little combination of values very nicely but what if I really want to press end let's say a value of 16 well I can come down here and say in and well to 6 well 19 I can simply say over here I can print them 18 so I can do that as well to check the range of values pretty nicely so if your value that you're looking for is supposed to be within a range of values of a collection you can just put that into the if it's in the collection also fairly easily you can do else and you can say you know in this case whatever that you gave me and of course if you send me something that I don't Stan Ward it is I'm just simply going to say whatever so in this case let's say string builder and you can see that in this case it simply is going to bounce back with a whatever well you can start putting these different combinations but one of the other things you can do here also is you can check for types if you really want to do so you can say is a string and then of course I can print over here you you got let's say you guard and then we'll just print out the string we have in this case input well let's kind of give this a try so we'll go ahead and call process but we'll send to it let's say the string hello but you can see that in this case because it's a type string it says you got hello well but on the other hand here comes a charm if I go up here and say you'll put dart length I get a compilation error because length is not a valid method on any so I cannot use in dart length however the language again avoids ceremony when you step into right here where the cursor is you already have proven that input is a string so it doesn't punish you it doesn't ask you to pay at all you can simply come here and say input dot length and be done with it that's a really beautiful auto casting as a result you're not wasting your time doing stupid casting the language is intelligent you programmed with the language like it's an adult right that's one of the things I really like about it rather than treating it like a child and you have to keep saying now that you transformed it are you gonna cast it you don't have to do that that kind of you know things remove the ceremony again from the code and make it very fluent and convenient so there's a really nice automatic casting similarly if you had a done if a statement to check if it something is now automatically it will cast over for that as well well moving a little forward from here we also have a null type which is really pretty cool no types are going to tell us whether a value could be null or not for example if I have a nickname over here and I want to take a name and return a nickname for this person and I'm gonna say a string over here but in this case I'm gonna say if name is equal to oh let's say in this case a Robert I'm to return in this example let's say a Bob but what about if there is a no nickname I'm going to simply return an empty string for now so if I print out a nickname over here and pass Robert over here you can see that in this case I'm going to get back the result of Bob but what if I print name and send a nickname thankfully vinkor doesn't have a nickname well in this case of course it doesn't really return anything but I really want to say that don't even bother giving a nickname for rank it so what do I do I'm gonna return a blunt null but we know that null is not very pleasant well as a result the code refuses to compile saying don't you dare do that you cannot return null anywhere you want to well this is one of the things from C sharp C sharp has nullable types and we can make use of that here in Cartland very elegantly so if you really think that you are going to return on null then put a little question after the type that becomes a nullable type and as a result you can return null values now similarly when you receive a data you can receive a nullable type as well from the call and then you can perform a check on the null but there are some really elegant syntax for checking as well so what you can do is if you have a nullable type on your hand whatever the variable name is we'll call it name you can put a question colon like this and this will be the part for if the value exists and then you can also put a little you know question : for the part where the value does not exist so you can write the syntax very elegantly to process that as well and again you will enjoy the auto casting I mentioned earlier once you go through the check you can then readily use the type as a regular type not as a nullable type that becomes really really powerful well what about creating functional style code well we can definitely create lambdas and play with them in here as much as in any other language so for example I'm gonna create a little lambda right here called double this lambda is gonna take an argument let's say element E because I'm trying threading the standalone I do have to specify the type char kind of like in Java so in this case I'm gonna return it e times 2 that becomes my little lambda expression I'm creating now I can say list off and let's say 1 2 3 a few different values I want to work with so what I can do in this case is I can then take this and say filter and to this I can say give an element element mark two is equal to zero I can get the elements which are in this case of course the even numbers then I can perform a map operation and I can just pass their double over to this and then finally a for each method and if you remember Java has a method called peek well here they have a method called you know each where you can say are each and then you can just peek into the variable as well so you can have a combination of these methods that are available to you well in this case of course I want to print this value out so how am I going to print it well I can say give an element I want to print the element so I'm gonna take all the even numbers double the even numbers and print them or if you don't want to really give a silly name to it you can simply say print align it and it is a legitimate variable like in groovy that you can use for lambdas as well so you don't have to really give a name if it's single variable you can just use the it carton also has a D structuring of lambda parameters as well so if you are receiving multiple parameters you can easily set them into different values much more easily let's talk a little bit about extension methods this is something I really enjoyed in c-sharp and you have beautiful extension methods here too so for example I'm gonna say value function called shout and I want to create this function whose name is shout well in this case we'll come back to this in just a minute but I want to go ahead and take a string so I'm gonna say greet is equal to hello but I want to call greet dart shout let's say but we know that shout does not exist on the object so as a result I'll get a compilation error right now so it doesn't know what shout is but I go back over here and say well this is on the string class so string dart shout and then I'm going to simply take this one and say well let's make it easier we'll just simply return to uppercase and ask you to return with the type inferencing and such so now when I run this code you can see it is going to convert it to uppercase and give it to us be very careful though while this is a very convenient feature you need to remember that this is not literally adding it to the class this is more of a compiler gimmick so what the compiler does says time it sees the word greed darts out Italy wires the call site right here so as a result if you have an overloaded method that would not work properly so in other words extraction methods cannot be overloaded it is simply just for you to conveniently call into it so there are certain limitations you have to be fairly comfortable with when you use this let's talk quickly about classes a little bit and see what we can do what classes are final when you begin to create them classes can have properties these are very much like your c-sharp properties you don't have to define fields they automatically get defined for you which is really nice so you can focus on the fields rather than focus on the properties rather than writing fields but what if I really want to do something with the field period you cannot define fields in Cartland however you can't refer to them you don't define them but you refer to them and you can refer to them using a name called field and of course you can also set the setters as private as well let's take a quick look at an example so class car over here and I'm going to define a property called year of registration and then I'm going to say in this case is equal to 2010 but this appears like a variable field but it's actually a property how do I create an object right now car equals to car there's no new keyword in Kotlin so um so there's a there's really a parentheses to really invoke it so you can treat classes like they are functions if you will to create objects of this now I want to print out right here card art year of registration and you can see that in this case we are pulling in the property but of course if you want to do some checking you can certainly do this so you can say sect value which is going to be a function I'm gonna create right here and what am I going to do within the function I'm gonna say if the value is greater than 2017 I'm gonna say throw run time exception and in this case I'm gonna specify not in the future right so you can specify a little exception otherwise what am I going to do field is equal to the value that you provided for me so we can start setting things very easily by these kinds of functions and you can write a setter for that but on the other hand if I tried to modify this and say well let's do this before we print it out so car dot year of registration is 2016 and you can see that in this case it does not prevent me from changing but if I try to set it 2019 I get an exception on it because I can't really change it so you can write setters for it and notice this is called field you don't really get access to the direct field again very similar to what c-sharp does but Carly I think does it a little bit more elegantly by using this thing called field where a c-sharp requires a actual name at the point so that's really nice over here you can also write it getter similarly so if I want to really bind this to yet another field I can write another getter for this and get that field through this particular variable so that works out really nicely but keep in mind one thing though that when you create classes classes are final to begin with you cannot inherit from them so you have to open them if you want to inherit what about static methods though so I'm gonna say object this is very much if you know Scala you're in home here with this so object util and I'm gonna define a function called get let's say number of course over here and this number of course is going to return to us let's say in this case because I'm going to just return the value let's just return a value of four let's say well I can call the util over here and call the get number of course method right directly here that's a static method on this particular class but if I'm creating an object what do I do well in this case I'm going to say class car right here but there's going to be defined as a car a class so this is a class as we can see we can write a function within here for example I'll save fun fun drive let's say and the drive function is an instance method as we would know so I'm going to simply say driving well this clearly requires an object to be used so I can say car equals to car and create an object in this case of course I can call the car drive very easily but what if I want a static method on this class well in this case of course if I want a static method on the class I can simply say card art you know some kind of a static method you know the kind for example well but the kind doesn't exist on the car right now how do i define a static method if I wanted to find a static method once again I'm gonna come in here and say companion and then this becomes an object and then within the companion object you can provide your kind method so fun and then there's going to be kind and let's just go ahead and print out in this case a kind called so um you can see how that becomes available as a companion object on the class you can also provide a name for your companion class also if it's a car kind for example what is the difference not nothing at this moment but if you want to get an access to this car kind how do you do this well if you want to get access to it you can say for example you know let's call this as ref is equal to well card art and I can say companion over here and that's a way for you to get access to the companion of that particular car but on the other hand if I were to go here and call this as a car kind in this case then no longer can you call it as a companion you would have to call it as a car kind if you give it a really you know specific name so there are different ways to get access to this particular companion object as well and you can use it and and one real detail if you want to really use your object as a data object maybe all you're doing is just pass data around not very much behavior you can say data class and then that just becomes an immutable class you can make a copy of it provides a few free methods like to string you can also access properties directly or using something called component and and get access to it also and finally about inheritance I quickly want to say that you have to open classes before you can inherit it but I want to talk about two very interesting features before we are done in the next six minutes so I want to show you a little bit about laziness so laziness is a pretty phenomenal feature I really like it so to understand this let's say we have a method called compute the method compute takes a value integer and all I'm going to do with this method is simply return that integer so I'll just return back the given value and let's say but what I'm going to do here is that I'm going to simply print out let's say this is going to say over here are called well you know one thing very clearly if I were to say X is equal to four and if I say if X is greater than five and if I say compute of let's say four is greater than seven I want to go ahead and print out let's say result well you know one thing very well when I run this code it is not going to print the result well that's great but what is it going to do though it's not also going to call the compute method after all because the compute method is short-circuited which is nice absolutely however if I take this one here and say Kemp over here and if I say Val temp is equal to that so such a sad thing it calls the compute method because it doesn't know that you don't need the value of compute so well you can do something really elegant here you can say Val temp by lazy and then you can use a level of indirection here and say that I want to postpone calling that until a later time so as a result now you can see it never actually called the compute method however if this becomes a forty-four clearly X is greater than five and when you run the code now you can see the computers called but not otherwise so you can do laziness very nicely well in a similar way this is just a specialized syntax of full-blown delegates that is available in Cartland a delegate simply contains a get value and a set value property doesn't have to implement any interfaces and it will just delegate with the call and it can do beautiful meta programming with it talking about meta programming I want to show you too elegant features which are pretty darn cool the first thing I want to show you here is let's say we have a class called pizza now in this case the class Pizza has a method of function called spread and I'm going to write an item which is a string let's say well in this case when I write this method I just simply want to call here and say oh well let's go ahead and say spread and then we'll just say item well I want to call this method after all so I'll say pizza is equal to pizza new pizza and once I create an object of the pizza ours a pizza Dart spread and let's say cheese I know this is a bad example to use right before lunch so when I run this code you can see it called it but you may say but wait a minute wouldn't it be so cool if we can do this so let's go ahead and try that sadly that doesn't work but we can fix it by saying this is an infix operation and we can get some beautiful elegant syntax with it so all you have to do is in fix a single parameter arguments can be used for it so we can start creating some elegant code with this kind of syntax so the final example I'll show you here used to apply this to do something a little bit bigger what if we can do something kind of like this what if we say operate and then I'm going to say it turns right and then it turns left and then it runs fast wouldn't that be cool to just write something like this well let's see how we can do this well func appro function operate and the function operate of course is going to take as an argument a lambda expression because remember this is just a lambda and how do I say it's a lambda I'd say func over here it's a lambda which doesn't return anything we'll start with this baby steps so operate is going to be called with that so we'll start with that and see what it's going to do so in this case I'm gonna call the operate and pass this to it it doesn't have a clue what it is but don't worry buddy we'll come to it so I'm gonna create a class called robot and the robot is gonna do all this work so let's say we have a method called let's just do the turns we're not worried about this right now so what about turns let's define a function well in fix function called turns and let's say it's going to take the variable in this case let's say direction but I'm going to simply call this and say old symphysis it turns and then we'll print the direction out here well let's do one more thing we'll just define a value called right and the right is going to be equal to right and then we'll define a value called left and this is going to be left we'll see how this is going to end up so now that I have the in fixed method and the turn method I'm gonna come in here and say this is going to take a robot as an argument and then return back a unit well okay so far so good we'll just I didn't say called over here and see what it's going to do at this point well when I run this code operate let's make sure this is correct so this is the operate it does expect you to spell correctly okay so that's the method we're gonna use and what about the turns right we'll come back to that in just a minute so now that we have this code running oh it's gonna call the called method but I need to invoke this particular method and have it run do some work in order to do this I'm gonna come in here and say func and then with this function I'm gonna create our object of robot so it's a robot is equal to robot and I'll say robot well we'll say with robot and I can perform this function to run within that context this is one way to do it carton also gives you a way for you to attach to this to a class also and you can run with it and by doing this you can say I want to run this code within the context of robot and so as a result I want to run that little code within that environment of execution well what this really amounts to is you can pass a lambda expression and have that lambda execute in this context let's quickly take a look at the error that is giving us no value passed as a parameter well sure enough that's correct we'll just pass the robot to it so now that we have gotten to that point right here what can we do next well remember this robot is coming in here and we know that it refers to a special name which is the name of the argument given to it and as a result what we can do here is we can start calling these kinds of methods what about the right well the right becomes a variable within this context of this object and that should become available right here as a dis start right and that should have actually picked it up I'm not sure why you didn't pick it up at this moment so in this case it will run in that context for now I'll put a string but we can leave this a homework or debug that so any one of this code of course you can see that it is turning right so you can get to this kind of fluency very nicely by playing with the language and passing lambdas around and and and then as a result using some infix notation you can gain quite a bit of fluency in this code if you download the code I provided you will see that I'm able to call this without the string itself and you will be able to use so I can start creating quite an interesting set of fluency as well and with all this said I managed to create a cover a very small part of the language so but it's a really fun language it's a lot of fun to play with it if you have enjoyed other languages I'm sure you'll enjoy Curtin as well if you want to download the examples please download from my website hope that was useful thank you [Applause] [Music] you
Info
Channel: Devoxx
Views: 47,209
Rating: 4.931551 out of 5
Keywords: DV17, Devoxx
Id: 7EVXypZDOos
Channel Id: undefined
Length: 51min 40sec (3100 seconds)
Published: Thu Nov 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.