Duration 39:24
16+
Play
Video

How to Kotlin - from the Lead Kotlin Language Designer

Andrey Breslav
Project Lead at Google
+ 1 speaker
  • Video
  • Table of contents
  • Video
2018 Google I/O
May 9, 2018, Mountain View, USA
2018 Google I/O
Video
How to Kotlin - from the Lead Kotlin Language Designer
Available
In cart
Free
Free
Free
Free
Free
Free
Add to favorites
217.44 K
I like 0
I dislike 0
Available
In cart
Free
Free
Free
Free
Free
Free
  • Description
  • Transcript
  • Discussion

About speakers

Andrey Breslav
Project Lead at Google
James Lau
Technology Product Management Leader at Google

Lead Language Designer and Project Lead of Kotlin. Andrey's whole career revolved around language engineering: he started at Borland, then did some academic work on DSLs and generative programming, taught software design and algorithms for a few years, then joined JetBrains in 2010 to co-found Kotlin.

View the profile

James is a product manager on the Android Developer team. He works on the Kotlin developer experience for Android. Prior to joining Google, he was a Lead PM at Microsoft where he spent over 10 years working on various developer-focused products. James graduated from the University of Waterloo with a Bachelor of Applied Science degree in Computer Engineering, and he also holds a Master of Engineering Management degree from Dartmouth College.

View the profile

About the talk

Kotlin is similar to the Java programming language, so it's natural that your Kotlin code looks very much like Java code when you are first start to use the language. While this is fine to begin with, you're probably not taking full advantage of all the language benefits. In this session, the lead Kotlin language designer will show you how you can write more idiomatic Kotlin, what the benefits are, and help you discover some of the most powerful yet lesser known features of Kotlin.

Share

Thank you. Thanks for being here this morning. My name is James and I'm part of the kotlin team at Google today. I have the pleasure of introducing a very special guests from brains really requires. No introduction. Now all of you know that Colin is now one of the most loved programming languages in the world. Google I know it's very rare for us to have external speakers, but this person was here last year and we invited him back because we couldn't think of anybody else better to teach kotlin other than one of the people who invented it. So, please help me welcome the

lead language designer for kotlin Andre breast love. President Reagan's election. Hello everybody. I'm very glad to see here, I guess and I really I'm going to do a live demo. So please bring my camera on. The reason why I have this horrible code of the slides is that we are all learning and our old habits sometimes get in the way. So I'll be presenting today on the topic of how do you get out of java habits and get to your coming habits that we all come from different backgrounds, of

course and many of us started with the Java programming language and build up our knowledge of programming through this. So we remember me things and have been inspired by major languages including the job. Never work like you can get your job done this way, but it can be in many cases has improved dramatically example, you can see here that I have a common class on the left and the other place on the right and it looks very similar. But this is definitely

not how we write cousin coat supposed to do is like remove all the announcers. What I have to say here is two properties one class. That's it. Right so I can try to transform it by hand, but I actually want to show off and Ice dual. I'm simply copy and paste the code from the job site of the common side. So it will use the Java to kotlin converter built into the ID and do it for me to Boom. There it is. A single line. That's actually all you needed to declare one class to property is all I

have here is the house to parameters of both of them are properties. That's all he wanted to say. This is one of the things that demonstrates how cheap declaring classes is in common and there's a consequence to this to look at this code here. It's obviously not how you're supposed to write code in any language. Actually. I wanted to forests a full name and your first name and last name and that's what I'm doing here. But how do I pack the results to put it out of the function? I don't have a way of returning to things from a function

have to put into one object and abusing a lift here. Just then awkwardly taking out one and the other two I make a first name and last name don't do this in any language. Logical reason to do any of this at least our old habits because they're driving classes is expensive right now. I have to create a new file for a lot of code in it. It's kind of awkward, but don't have to do this. I'll need to say my class full name with first and last names as properties. and then all I need to do here is just returned

that right, so My full name. Goes and now instead of indices I can say first and last right here. So that's the idea of being Chief Keef Amy you like time of Declaration say it's saving you mental effort. You can represent you or multiple return as a class and it doesn't cost you anything different this you'll see that my people doesn't work obviously because this is single line class. And so now I'll go to declare there right now. They're both but I really don't need to do this in common because you probably know that there is something called dataclasses, right? Who knows out of

classes. How many people good so so you know that I sent for this single key word there and they can probably generates many things for me. It's because of his half goat stew strength and many other candy and that's it change your mind about how expensive a class is I can use it easily in all your abstraction warm-up laps. Look at something else properties. So we talked about classes will go through properties and then go over to function properly done the way

you shouldn't do it in, again before we're kind of want one liners. We're both got her interest reveal better. You definitely don't Define functions for that. You have your customer service centres index as you probably know. There's no doubt glasses know that you have filled to You will write your back in storage that sits. You don't need to introduce extra names and anything else. That's straightforward right, but then just go. So curious already has a sensible logic. I have two properties one of them private and malleable. Immutable and on my first

access I'm checking stuff now and then I compute the value and right into it and then I output returning from I get her. It's a lady property, right? I personally wrote dozens and dozens and toes and job in the other languages. Kind of bored by that and that's why comment has an instructor mechanism called application for properties. How do you get a rid of all the repetition of this lazy logic only care about is this expression in math my property by just lazy

all this this is what having now I'm saying my property is not simply lied by something its delegated you with this lazy. Thank you and upon first access. This lender will be executed and then the rest will be stored by the library. The lazy is not a language constructs. It is just a library functional. You can to find your own and I do with many other things so they take away here is that if you have a common kind of property like observable, for example, when you need

to be notified that something was modified use the library or write your own secure delegates out of their bowl. Does the job from the library but if you like you don't have to like this when you have one property and then the other property and the other doing the same thing over and over again I'll need to do is this actually declare a single class that encapsulates the logic of your property like you're better than generic Center and that's it. You can now simply refer to this class in many properties and get your business

logic database base access all kinds of validation anything you like. I can be extracted as a library and then reuse across your project. That makes sense. Are you know this already? I got some a people you actually shoot. I'm sure you can benefit from this so hers and now I've got to function again. This is very horrible coat. Don't try code like this in common pleas. This is very much inspired by our habits in the Java programming language when I have to

put everything into a class right string guitar. Does your project has its own string guitar class? So if it doesn't is just a very new project, right? Any of my projects have them but the thing is in, you don't have to use a class or first of all, I don't have Statics to use these functions from this class. You have to say a string tail parentheses, which makes a new object right? I don't want any object over time like this. It's all a bit of an improvement in in my Insanity. Right? So I was praying

that whatever time I wanted to call function, that's crazy, but I don't need any enclosing container at all because I have top-level functions functions. What are they? They're just decorations, right? But have them only in classes that many people learn this and William is so this is a lot more but it's still not great in terms of here. You have to overload, right? First word is supposed to Farson string find the first space and take the first word in truck or turned right separator is not a space but it, or something or is it more

efficient version? And then this is how you call it. Actually in most contacts right here is just a default value used to using overloads for this and also some people use nullable parameters like pass and null here and I'll give you a default value don't do this in, you don't need to know. All you need to do actually is simply specify your default ID for this space here. That's it. Rancho Auto there was no need to emulate Vehicles. They are both into the language and same for when you have many many default parameters with

different values like multiple Boolean so you can just use named Brian or syntax. Express which of them he actually need and all of them all of the rest will be used by default. It makes functions. You were the first place and then OK Google functions, right actually is kind of function is kind of midway between be like the color and style and the Java style because it's actually very much a good idea to put essential string class only it's not because the string class is not

controlled by you we can put everything into the swing class and I really want to keep this train. So what I would really like to do things like this, I can say my string get first word and that's it. Right, so it's much like a method. It's calling extension function. Actually. It's not sitting in Spring Class. I didn't go into the jdk and alter the class. I can't control like that sell. The mechanism you can use I'll do it manually to illustrate how work here. So I have a receiver have touched ring now. Don't be there forever anymore

and I can say this here and use my best here or am it all of this on the left hand side. So now I'll be able to use It doesn't make sense. I can do the same with a property actually would be very nice to do it. This way just have first word as a property name. You can have an extension property. But otherwise you're good to go. And that's it. So that's your functions properties classes really minimal. Look at the string class. It's only five methods. If you can bear that the job I hope he screams and screams of decorations. So you can keep your ATM in the mall

and all the utility functions can be extensions can some different libraries to be modularized like this and that's a very important. tool for designing apis Do you have questions? Okay, I couldn't take them anyway. I got now that's a look at this and I'm doing something very difficult and traversing a higher key. So I have containers and leaf elements containers can be there and I want to extract all the texts from retired priests pray for my classes are three lines of code. Not much. There is an element.

So I'm using extension functions, I'm using functions and everything as I told you so it's it's right, but I don't like this code. Why do like it here to Traverse a higher key? I needed. Right. So it so I need to pass the stringbuilder down to stack and add to it as I am going down the tree. But then I end up with the top of a function that's only needed by this one here right? There was this one is not free lead anywhere but inside this function. So what I'd really like to do is just put it inside. Just go here and make it a

local function. Yeah, it's just expressing that nobody else needs. If you don't need private helpers anymore a look for local helpers a little bit. You can actually make use of closure so I can create my stringbuilder right here. And get rid of all this so I don't need to return or take Bronner's here. All I need here is used whatever is declared above. And then I just do extract exstrophy right here and return dreambuilder to strength extract. Text Ashley Furniture. No, sorry.

How goes like you can turn something into local function and loveridge closure sew with this terrible? Is it clear outside my function to anyone outside the outer and I'm using here and that's it. Now local functions extension functions of level functions default parameters use these they will make your covid-19 know it's looking what's still there? Lisa gray code ricota's giftless the idea of the compiler show you that something is not needed there and it's actually isn't this gas is redundant because

we have here and I don't know if you see it you do but the text variable is god-breathed why dream is because they can fire can figure out the cash for you. It's actually much they only can be annoyed my cast all of all over the place, right? So I know it's text why don't you know, well now it knows and actually you don't need this terrible either because it's the only usage the same thing here and then my container going to be mine as well. So here it is

a smartcast. It makes it safer more concise. And actually it makes all the cast that still are in your program meaningful. So when you seeing as operator in common now, you know, it means something if not just a useless compliment compliment to the is Jack about this thing here is what I'm doing. I'm just applying the same function do everything in a single function. So what what I want to do is something like this. It's a little bit nicer looking. And then let's talk about we have a

hierarchy. I have my leaves on my containers and that's what I want Express right? I'm checking different cases there to do that. It's a lot nicer to use I went in when they opened when can switching types right here. But they're going to know anything about it and it's coming from my old habits on declaring at close higher key. I have only containers and text right? I don't have anything else. But now I have this pretty annoying Alice case right here. Why because the compiler has no idea. I don't have anything but containers at 6. It's just an abstract

class and I have some place in there, but you can actually Express in Pittsfield. I didn't have a seal class which means all the supplies are known you can declare them outside this file. And this way the idea of the compiler know that this else is useless from like almost two screens of code two lesson one simply applying the idiom the problem to this code. You have questions. I'm sorry. All right. I don't know. That's just continue with this exercising and look at them some more examples of Expressions that are with

black old habits in mind and will try to transform them. I got something better. Go first thing that's really stands out here is far never use of our Warriors are useful immutable variables can be used for many nice things, but it's kind of discouraged. If you need to bore. You need a very good reason. He was no good reason using felt definitely hear its repetition repetition is ugly repetition is error-prone, especially if this was not a single name, but many things changed,

so I would like to get rid of this repetition X does anyone remember Pascal? Pascal anyone have a good good. I started then passed out almost had this thing which was a book and construction, and it's a function you can use it and here we can get rid of all the acts things here. Just like this and now it looks even more stupid right? I'm just a sign into the same variables. Don't do that. Okay, so and now how to print line with strength was something to drink with something from for

something. It don't words most languages now have string interpolation probably have that as well. So what you actually need here is this Okay done with this one important things into your scope with with your string interpolation. It's nice. Now, I'm sure I'm praying you map the old way. I can kind of make it a little nicer like this. By using my operators, but it's really much nicer if I just use a booger function. So what I can do here he is replaced all my map for things with

pears. Not perish but pair. Sorry. 30 minutes when talking is difficult, usually use it using the to function Hilton Operator just Library function in math the map you can say Yorkie and value and just have your variables like this which makes for Loops a lot more concise this example of code with my if statement is something I really hate to buy my my code in Java because it's like he's assignments here. They all fall apart. So I really like to do things like

this in common. So if and many other things are actually Expressions, this is something for the C language family. We are used to divide the outer covering two statements and expressions write statements are things that have a sex expressions are things that have values. So you assign Expressions two variables and write statements to science things to things. So common is halfway between the oral tradition and functional tradition that we have a lot more Expressions that you're used to in other languages so you can do this. Of course, you don't have to

use a far you don't have to make it a different line. You can assign it right away. So by the way, the expression is the last thing in the block Tell the same for what when is not simply switch disc case on steroids s largely importantly an expression so you can also do it like this, right? So not many returns here, but one returns here will be a lot nicer. If you don't have to repeat yourself, of course this much and you can say Eva Mendes. By the way, if you want to check if something's I didn't even don't do like me. It's only for demo purposes don't-try-this-at-home. It will hurt.

Yeah, so this one can be further simplified like this. So again trying to remove the noise when it when you see color like this is a nice noise is harmful for your break. Nothing just a quick demo of what to do with no ability. So he's question marks quiz familiar with notable types in common. How many people I'll go really really quick so you can have multiple types and compiler makes you do things like this. So now you can either do this, which says just safe lead reference me. I'm

With You by the waiting to hear as well. Rights are you don't have to write an if rounded and you can actually simplified like this. Another nice thing is that you can use as an Elvis operator like this. SO2 to simplify your like longer if statements into something and this is kind of curious because this is so how this works out mistakes and expressions left hand side of string asks. Are you annul really nicely and then if it's a no evaluates right hand side, right? It's basically it's supposed to be a default sound

like if you are now on left hand side like you the default on the right hand side, but your people can't be just the Richer. Which means that you don't compare and evaluate their your jump out of the function. That's a quite interesting thing from the Texas Simpson point but I'm not giving electric here. I'm doing a demo. OK Google Expressions, let's look at some functional style people very often referred to as a functional language. Actually. I think Colin is a multi Paradigm language that Sports functional

style. You don't have to do my Java Aldi's in mind. I wrote this code which is just goes over a list of numbers and fix those that are divided by 16 and then convert them to text. What is actually does. Is filter map right now is this one and filter is this one of my ID? I can do this. Tell newer versions of all programming languages have something like this can definitely leverage. This filter is a function. This Lambda is a function value. You don't have by the way to declare it's as if they're a bunch of can get rid of it

from semi functional things like you can take anywhere in your Kodi can't a also have this tell you also do this for me, please like friends. If this list for me and then proceed with what you were doing like nevermind this it's just excited and this is very handy for bugging. You don't have to break your chain Apartments. One thing about functional abstractions in non functional languages take the number of times. I want to repeat something and if something is a function, by the way,

you don't have to invent your own function interface every time just use the function types here. It's a function that takes an insert as unit is something you don't care about Repeat that rhyme repeat. I'm always there much conscious about I was going to cost me right so it's a function. It takes a Lambda as a barometer. So it's actually just another barometer calling customers right outside the parentheses because it looks more like a language conference like this. But then okay, I'm

running this. I have to create a Lambda object right to object to every time I do anything like this. So there's a cost of this abstraction. It's nice code. I can reuse things. I can arrange liposuction in my code, but there is at all on that. Actually, you can very often get rid of this all of grading Lambda Lambda object for you, but just using inline function right to cure. Nothing happened at the coal side that I can see if I say, she'll call me by Coco. And to decompile listen to Java just to scare you a little bit. It was

much easier to talk so far. So if I do this There goes it's a symbol for love. BridgeMill Antigo Well, the compiler simply optimized it the way you don't need a louder right there with you. If you simply have your Loop here and you in line everything you end up with a loop. That's it. The big difference in the mindset when you go from the Java programming language. The common programming language is that he has but has a really free and by the way, if he's all are free to so many visitors

center library free attractions. You don't have to pay for calling them. It's just code generator for you. The functional in Carlin is not only convenience, but also white cheap, by the way. Let's look at this example. So sure. I'm trying to do a parallel complication. Well, if it's a stupid Temple, nobody does feral complication is there ever again with my old habits in mind? I'm craving a hundred thousand threads. how to talk and read features which does some work actually sleeps for one second then

to Sprint's number and then I have to join all the threads to my main friend What's up? Java Lang out of memory error Basically what it's telling me hey, you cannot create a hundred thousand threads. Are you crazy there? It's Howard thousand snacks into memory. Just get reasonable. And that's here like OS threads are not cheap. You have to allocate resources for Threads. So you don't do that's the only thing is I have this example, who knows about corgis.

Good who uses them in Russian? Okay soon enough you all over using I'm sure that'll work. They're the same code so I'll just friends here. I'm crazy async task switcher using chords. There is no out of memory. It's raining all the numbers and I'm good. So again, introduced for jeans as a means of making your essay competitions nicer and that works, but I'll what's the cost of that. So the cost of that is at least cheaper than having a thread for each computation

Nobody Does that exactly but still and doesn't cost you nearly as much as anything like that something has a really good for right here. so dreads Allegacy interface orpat out or more during face, whatever you have to do to make things a synchronous or make things right? So just ask me to do something. I'll do it for you and I'll let you know when I'm done there sure. I have my service request and a callback function. That's past winter when the work in the comments are done calling to call back and just passing my answer there

for everyone rights. But this is what the code look like when I want to exchange messages between two services. So I just want to basically the same two messages in sequence. And here's what I have to put the first request than a call back. This is a result of their class. I printed then next request inside the call back and then print inside till you see the staircase right here right? One step one step two steps three steps and you can actually get down this Staircase which is not nice

to do something. But what if you needed to do like and calls this number like make a list of calls, so I came up with Which is nice at all because I have to have to call back and try to call back inside a call back. Right so I could come up with a memory place in terms of time is quadratic come up with something. So what do we want to do to be able to do it as something like this? I just say okay send one request with results send the other request and then if I want to repeat something to compete with a for Loop, right? The best coach here is actually using the same

callbacks only the accordion abstractions are just ring this way from me. So you can take any call Big Bass API you have now and turned into this. I think it's straightforward with the vest is calling the same Services because I have this function right here. So what I'm doing, I'm just turning the request into suspension function. Through this simple contract extension function of my callback service. I say the first thing I say there is to spend my Corgi. So I must I'm

assuming I'm in a quart team suspended right away. I got my continuation and that's it. I'm suspended. I'm waiting for a request. So there it is. And when the request is done, I just say you my clarity that's it. So this simple lines of code turns your call back Bays API intercourse 1080i and makes so make this sorry makes this code into this which is a lot more readable to my sense. How do you like it? Thank you. Actually, do you want to do to be a lot more this year? And I'm sure your wants you or you need to catch exceptions so

catching. Helen your exceptions is very important and that's as easy as this just cash your exception whatever happens on the happens with your Where your request just catch it and resume with exception. So I propagate exceptions through your jeans. They're nicely and you'll be able to try catch around here light surrounds us with try catch and catch us like this. I think I owe file systems. Are you can do background threads everything you need. Dell is it nice. And I guess the last example I'll be showing you today.

Is this one it just another show off for how corgis can help you. Take a look. What I want to do is degrade an infinite stream of numbers of numbers. I eat them for breakfast. I want just a Fibonacci Sequence between major and then I can take 20 of them a hundred mm filter Maps lights, whatever so it's all in the same Fred. What does it take Goldenseal statements from here and just put them in the sequence sequence if I want to say if GMP is greater than 10? Continue I can skip Pieces of my

logic so that's as straightforward as any quarantine gives you a lazy sequence. Okay. So takeaways classes are cheap functions are top-level or local. No overloading to emulate your default has any Properties or genes? Have a nice coffin? And I want to advertise some more activities today. So if you still have questions, I couldn't take you can come over to an office hour. We were having at 12:30. You can come over to a Sandbox area see where we're at the booth some of the day at least and right after my talk. There will be a

talk by Jake Warden about the Android KDX, which is very exciting. I believe it's on stage to the welcome their. Thank you very much for your attention.

Cackle comments for the website

Buy this talk

Access to the talk “How to Kotlin - from the Lead Kotlin Language Designer”
Available
In cart
Free
Free
Free
Free
Free
Free

Access to all the recordings of the event

Get access to all videos “2018 Google I/O”
Available
In cart
Free
Free
Free
Free
Free
Free
Ticket

Interested in topic “Software development”?

You might be interested in videos from this event

September 28, 2018
Moscow
16
159
app store, apps, development, google play, mobile, soft

Similar talks

Jake Wharton
Software Engineer at Google
Available
In cart
Free
Free
Free
Free
Free
Free
Lukas Bergstrom
Product Manager at Google
+ 1 speaker
Yigit Boyar
Software Engineer at Google
+ 1 speaker
Available
In cart
Free
Free
Free
Free
Free
Free
Richard Uhler
Software Engineer at Google
Available
In cart
Free
Free
Free
Free
Free
Free

Buy this video

Video

Access to the talk “How to Kotlin - from the Lead Kotlin Language Designer”
Available
In cart
Free
Free
Free
Free
Free
Free

Conference Cast

With ConferenceCast.tv, you get access to our library of the world's best conference talks.

Conference Cast
558 conferences
22059 speakers
8245 hours of content