Kotlin - a JUG.RU talk, 2016

36
Andrey Breslav

Transcript of Kotlin - a JUG.RU talk, 2016

Page 1: Kotlin - a JUG.RU talk, 2016

Andrey Breslav

Page 2: Kotlin - a JUG.RU talk, 2016

http://kotlinlang.org

Page 3: Kotlin - a JUG.RU talk, 2016

JetBrains

Page 4: Kotlin - a JUG.RU talk, 2016

• Pragmatic language for industry– Started in 2010– Open Source– 1.0 Released in Feb, 2016

• For JVM, Android, and JavaScript– Interoperability: 100%

• Try online: https://try.kotl.in

Page 5: Kotlin - a JUG.RU talk, 2016

bytecode

Kotlin

Page 6: Kotlin - a JUG.RU talk, 2016

Technicalities

• Few compiler optimizations – inlning as a language feature

• Compiler intergated into the IDE

• Incrementality

Page 7: Kotlin - a JUG.RU talk, 2016
Page 8: Kotlin - a JUG.RU talk, 2016

Requirements

• Statically typed

• “Toolable” (is this a word at all?)

• Interoperable (yes, C++ has been there!)

• Learnable (“democratic”)

Page 9: Kotlin - a JUG.RU talk, 2016

Comparing to others

Page 10: Kotlin - a JUG.RU talk, 2016
Page 11: Kotlin - a JUG.RU talk, 2016

Over 500K LOC of Kotlin:

IntelliJ IDEARider (C# IDE)

YouTrackJetBrains Account

(and more)

Page 12: Kotlin - a JUG.RU talk, 2016
Page 13: Kotlin - a JUG.RU talk, 2016

In Open-Source Projects, Kotlin itself excluded

Page 14: Kotlin - a JUG.RU talk, 2016

In Open-Source Projects, Kotlin itself excluded

Page 15: Kotlin - a JUG.RU talk, 2016

IDE

Compiler

(Incremental)Build System

Page 16: Kotlin - a JUG.RU talk, 2016

Java*.kt *.java

compiler

Javacompiler

Page 17: Kotlin - a JUG.RU talk, 2016
Page 18: Kotlin - a JUG.RU talk, 2016
Page 19: Kotlin - a JUG.RU talk, 2016

“Billion dollar mistake”— Sir C. A. R. Hoare

Page 20: Kotlin - a JUG.RU talk, 2016

Java String s = null;

s.length();

Errors At Runtime

Kotlin val s: Strings.length()

val s: String? = nulls.length()

Errors At Compile Time

= nullNullable

type

Page 21: Kotlin - a JUG.RU talk, 2016

Check and use val s: String? = …if (s != null) { s.length()}

Check and exit if (s == null) return

s.length()

Rock’n’Roll s?.length()s!!.length()(s ?: “…”).length()

Page 22: Kotlin - a JUG.RU talk, 2016

Kotlin is good with nulls

Page 23: Kotlin - a JUG.RU talk, 2016

What about Java?

Page 24: Kotlin - a JUG.RU talk, 2016

Java (as seen from Kotlin)public class JavaClass { public String foo(List<String> l) {…}}

String

String?

List<String>

List<String?>

List<String>?

List<String?>?

Safest!

Page 25: Kotlin - a JUG.RU talk, 2016

Java Interop: All Nullable

javaValue.toString().length() + 1

javaValue?.toString()?.length()!! + 1

val l: List<String> = javaValue?.getList()!!

ArrayList<String?>

Page 26: Kotlin - a JUG.RU talk, 2016

Your safest option doesn’t

work!

Page 27: Kotlin - a JUG.RU talk, 2016

Annotationspublic class JavaClass { @NotNull public String foo(@NotNull List<String> l) {…}}

String

String?

List<String>

List<String?>

List<String>?

List<String?>?

Page 28: Kotlin - a JUG.RU talk, 2016

Annotations are cumbersome

ANDdon’t really help!

Page 29: Kotlin - a JUG.RU talk, 2016

Pick Two

• Null-safety• Convenience• Java Interop

Platform Types!

Thanks toDr. Ross Tate of

Page 30: Kotlin - a JUG.RU talk, 2016

Java: Platform Typespublic class JavaClass { public String foo(Bar<String> l) {…}}

String! Bar<String!>!

Platform Type

Page 31: Kotlin - a JUG.RU talk, 2016

Dereferencing Platform Values

s: String s: String? s: String!

s.length() s.length() s.length()

s?.length() s?.length() s?.length()

s!!.length() s!!.length() s!!.length()

NPE

Page 32: Kotlin - a JUG.RU talk, 2016

Assignability

String String?

String!

Platform Type

Not Transitive!NPE

Page 33: Kotlin - a JUG.RU talk, 2016

Some Notes

• Platform Types are Not Denotable!– String! is notation, not syntax

• Pure Kotlin is Null-Safe• Kotlin+Java is as safe as Java

• Annotations Still Applicable– @NotNull String in Java becomes String in Kotlin

• Dynamic Type for JavaScript Interop– JS is even unsafer than Java

Page 34: Kotlin - a JUG.RU talk, 2016

Nullable types: Summary

Cost• More Complexity• Less Null-Safety

Benefit• Smooth Interop

Page 35: Kotlin - a JUG.RU talk, 2016

http://kotlinlang.org

Page 36: Kotlin - a JUG.RU talk, 2016

Images Used

• http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-32-02-metablogapi/8054.image_5F00_thumb_5F00_35C6E986.png

• http://www.complete-coherence.com/wp-content/uploads/2015/06/Is-business-rational-or-emotional.jpg

• http://www.mightyheaton.com/wp-content/uploads/2012/05/33-1024x908.jpg

• https://openclipart.org/image/2400px/svg_to_png/97543/text-file-icon.png

• http://fileinfo.com/img/icons/files/128/class-737.png

• http://1.bp.blogspot.com/-UK5uEhDELUA/T9duhHrDeII/AAAAAAAAB20/nKpYXYhqub4/s1600/application-x-jar%5B1%5D.png