Kotlin techtalk

download Kotlin techtalk

of 72

  • date post

    20-Aug-2015
  • Category

    Technology

  • view

    1.230
  • download

    7

Embed Size (px)

Transcript of Kotlin techtalk

  1. 1. THE KOTLIN PROGRAMMING LANGUAGE Sergey Lukjanov, 2012slukjanov@mirantis.com1Thursday, February 16, 12
  2. 2. WHAT IS KOTLIN? JVM-targeted Statically typed Object-oriented General purpose Programming language Docs available today Open source from Feb 142Thursday, February 16, 12
  3. 3. OUTLINE Motivation Design goals Feature overview Basic syntax Classes, types, inheritance High-order functions Generics Tuples Pattern matching Class objects3Thursday, February 16, 12
  4. 4. MOTIVATION4Thursday, February 16, 12
  5. 5. MOTIVATION IDEA codebase 200MB Java-code, 50k classes 4Thursday, February 16, 12
  6. 6. MOTIVATION IDEA codebase 200MB Java-code, 50k classes Java libraries and community 4Thursday, February 16, 12
  7. 7. MOTIVATION IDEA codebase 200MB Java-code, 50k classes Java libraries and community There are many languages, why not try? 4Thursday, February 16, 12
  8. 8. DESIGN GOALS5Thursday, February 16, 12
  9. 9. DESIGN GOALS Full Java interoperability 5Thursday, February 16, 12
  10. 10. DESIGN GOALS Full Java interoperability Compiles as fast as Java 5Thursday, February 16, 12
  11. 11. DESIGN GOALS Full Java interoperability Compiles as fast as Java Safer than Java 5Thursday, February 16, 12
  12. 12. DESIGN GOALS Full Java interoperability Compiles as fast as Java Safer than Java More concise than Java 5Thursday, February 16, 12
  13. 13. DESIGN GOALS Full Java interoperability Compiles as fast as Java Safer than Java More concise than Java Way simpler than Scala 5Thursday, February 16, 12
  14. 14. FEATURE OVERVIEW 1/2 Static null-safety guarantees Traits First-class delegation Properties (instead of elds) Reied generics Declaration-site variance & Type projections High-order functions (closures) Extension properties and functions Inline-functions (zero-overhead closures) 6Thursday, February 16, 12
  15. 15. FEATURE OVERVIEW 2/2 Tuples Modules and build infrastructure Pattern matching Range expressions String templates Singletons Operator overloading Full-featured IDE by JetBrains Java to Kotlin converting 7Thursday, February 16, 12
  16. 16. CODE EXAMPLES8Thursday, February 16, 12
  17. 17. HELLO, WORLD! fun main(args : Array) : Unit { println("Hello, World!"); } fun println(any : Any?) /* : Unit */ { System.out?.println(any); } 9Thursday, February 16, 12
  18. 18. HELLO, ! fun main(args : Array) { var names = ""; // names : Stringfor(idx in args.indices) {names += args[idx]if(idx + 1 < args.size) {names += ", "}}println("Hello, $names!") // Groovy-style templates } val Array.indices : Iterable get() = IntRange(0, size - 1)10Thursday, February 16, 12
  19. 19. HELLO, ! (FASTER) fun main(args : Array) { var names = StringBuilder(); // names : StringBuilderfor(idx in args.indices) {names += args[idx]if(idx + 1 < args.size) {names += ", "}}println("Hello, $names!") // Groovy-style templates } fun StringBuilder.plusAssign(any : Any?) { this.append(any) }11Thursday, February 16, 12
  20. 20. HELLO, ! (REALISTIC) fun main(args : Array) { println("Hello, ${args.join(", ")}!") } 12Thursday, February 16, 12
  21. 21. HELLO, ! (REALISTIC) fun main(args : Array) { println("Hello, ${args.join(", ")}!") } fun Iterable.join(separator : String) : String { val names = StringBuilder() forit (this) { names += it.next() if (it.hasNext) names += separator }return names.toString() ?: "" } fun forit(col : Iterable, f : (Iterator) -> Unit) { val it = col.iterator() while (it.hasNext) f(it) }12Thursday, February 16, 12
  22. 22. NULL-SAFETY 1/2 fun parseInt(str : String) : Int? { try { return Integer.parseInt(str) } catch (e : NumberFormatException) { return null } } 13Thursday, February 16, 12
  23. 23. NULL-SAFETY 1/2 fun parseInt(str : String) : Int? { try { return Integer.parseInt(str) } catch (e : NumberFormatException) { return null } } fun main(args : Array) { val x = parseInt("1027") val y = parseInt("Hello, World!") // y == null println(x?.times(2)) // cant write x * 2 println(x?.times(y)) // times argument cant be nullable println(x?.times(y.sure())) // throws NPE if y == null if (x != null) { println(x * 2) } } 13Thursday, February 16, 12
  24. 24. NULL-SAFETY 2/2 fun String?.isNullOrEmpty() : Boolean { return this == null || this.trim().length == 0 } 14Thursday, February 16, 12
  25. 25. NULL-SAFETY 2/2 fun String?.isNullOrEmpty() : Boolean { return this == null || this.trim().length == 0 } fun main(args : Array) { println("Hello".isNullOrEmpty()) // false println(" World ".isNullOrEmpty()) // false println("".isNullOrEmpty())// true println(null.isNullOrEmpty())// true } 14Thursday, February 16, 12
  26. 26. AUTOMATIC CASTS fun foo(obj : Any?) { if (obj is String) { println(obj.get(0)); } }15Thursday, February 16, 12
  27. 27. WHEN STATEMENT fun foo(obj : Any?) { val x : Any? = when (obj) { is String -> obj.get(0) // autocast to String is Int -> obj + 1 // autocast to Int !is Boolean -> null else -> "unknown" }val i : Int = when (obj) {is String -> if(obj.startsWith("a")) 1 else 0is Int -> objelse -> -1} }16Thursday, February 16, 12
  28. 28. TYPES 1/2 Syntax Class types ListNullable types Foo?Function types(Int) -> String Tuple types(Int, Int)Self types This 17Thursday, February 16, 12
  29. 29. TYPES 2/2 Special types TopAny?Bottom NothingNo meaningful return valueUnit 18Thursday, February 16, 12
  30. 30. TYPES HIERARCHY Any?Int?String?...AnyNothing?Int UnitNothing Complete lattice 19Thursday, February 16, 12
  31. 31. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  32. 32. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  33. 33. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  34. 34. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  35. 35. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  36. 36. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  37. 37. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  38. 38. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  39. 39. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  40. 40. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  41. 41. MAPPING TO JAVA TYPES KotlinGEN Java LOAD KotlinAnyObject Any? Unit voidUnitIntint Int Int?IntegerInt?String StringString?ArrayFoo[] Array?Arrayint[]Array?List List List? Nothing - -FooFooFoo? 20Thursday, February 16, 12
  42. 42. CLASSES open class Parent(p : Bar) { open fun foo() { }fun bar() {} } class Child(p : Bar) : Parent(p) { override fun foo() { } } Any is the default supertype Constructors must initialize supertypes Final by default, explicit override annotations21Thursday, February 16, 12
  43. 43. TRAITS trait T1 : Class1, OtherTrait { // no state } class Foo(p : Bar) : Class1(p), T1, T2 { // ... } class Decorator(p : T2) : Class2(), T2 by p { // ... } 22Thursday, February 16, 12
  44. 44. DISAMBIGUATION trait A { fun foo() : Int = 1 // open by default } open class B() { open fun foo() : Int = 2 // not open by default } class C() : B(), A { override fun foo() = super.foo() // returns 1 } 23Thursday, February 16, 12
  45. 45. FIRST-CLASS FUNCTIONS fun foo(arg : String) : Boolean // function (p : Int) -> Int // function type (Int) -> Int // function type (a : Int) -> a + 1 // function literal (b) : Int -> b * 2 // function literal c -> c.times(2) // function literal 24Thursday, February 16, 12
  46. 46. HIGH-ORDER FUNS fun filter( c : Iterable, f: (T)->Boolean):Iterable filter(list, { s -> s.length < 3 }) 25Thursday, February 16, 12
  47. 47. HIGH-ORDER FUNS fun filter( c : Iterable, f: (T)->Boolean):Iterable filter(list, { s -> s.length < 3 }) filter(list) { s -> s.length < 3 } 25Thursday, February 16, 12
  48. 48. HIGH-ORDER FUNS fun filter( c : Iterable, f: (T)->Boolean):Iterable filter(list, { s -> s.length < 3 }) filter(list) { s -> s.length < 3 } // if only one arg: 25Thursday, February 16, 12
  49. 49. HIGH-ORDER FUNS fun filter( c : Iterable, f: (T)->Boolean):Iterable filter(list, { s -> s.length