THE KOTLIN         PROGRAMMING LANGUAGE                             Sergey Lukjanov, 2012                            slukj...
WHAT IS KOTLIN?         •JVM-targeted         •Statically typed         •Object-oriented         •General purpose         ...
OUTLINE         •Motivation         •Design goals         •Feature overview         •Basic syntax         •Classes, types,...
MOTIVATION                            4Thursday, February 16, 12
MOTIVATION         •IDEA codebase ≥ 200MB Java-code, ≥ 50k classes                                 4Thursday, February 16,...
MOTIVATION         •IDEA codebase ≥ 200MB Java-code, ≥ 50k classes         •Java libraries and community                  ...
MOTIVATION         •IDEA codebase ≥ 200MB Java-code, ≥ 50k classes         •Java libraries and community         •There ar...
DESIGN GOALS                            5Thursday, February 16, 12
DESIGN GOALS         •Full Java interoperability                                       5Thursday, February 16, 12
DESIGN GOALS         •Full Java interoperability         •Compiles as fast as Java                                       5...
DESIGN GOALS         •Full Java interoperability         •Compiles as fast as Java         •Safer than Java               ...
DESIGN GOALS         •Full Java interoperability         •Compiles as fast as Java         •Safer than Java         •More ...
DESIGN GOALS         •Full Java interoperability         •Compiles as fast as Java         •Safer than Java         •More ...
FEATURE OVERVIEW 1/2         •Static null-safety guarantees         •Traits         •First-class delegation         •Prope...
FEATURE OVERVIEW 2/2         •Tuples         •Modules and build infrastructure         •Pattern matching         •Range ex...
CODE EXAMPLES                            8Thursday, February 16, 12
HELLO, WORLD!         fun main(args : Array<String>) : Unit {             println("Hello, World!");         }         fun ...
HELLO, <NAMES>!         fun main(args : Array<String>) {             var names = ""; // names : String                    ...
HELLO, <NAMES>! (FASTER)         fun main(args : Array<String>) {             var names = StringBuilder(); // names : Stri...
HELLO, <NAMES>! (REALISTIC)         fun main(args : Array<String>) {             println("Hello, ${args.join(", ")}!")    ...
HELLO, <NAMES>! (REALISTIC)         fun main(args : Array<String>) {             println("Hello, ${args.join(", ")}!")    ...
NULL-SAFETY 1/2         fun parseInt(str : String) : Int? {             try {                 return Integer.parseInt(str)...
NULL-SAFETY 1/2         fun parseInt(str : String) : Int? {             try {                 return Integer.parseInt(str)...
NULL-SAFETY 2/2         fun String?.isNullOrEmpty() : Boolean {             return this == null || this.trim().length == 0...
NULL-SAFETY 2/2         fun String?.isNullOrEmpty() : Boolean {             return this == null || this.trim().length == 0...
AUTOMATIC CASTS         fun foo(obj : Any?) {             if (obj is String) {                 println(obj.get(0));       ...
WHEN STATEMENT         fun foo(obj : Any?) {             val x : Any? = when (obj) {                 is String -> obj.get(...
TYPES 1/2                                             Syntax                             Class types               List<Fo...
TYPES 2/2                                     Special types                             Top                      Any?     ...
TYPES HIERARCHY                                         Any?          Int?                String?      ...        Any     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
MAPPING TO JAVA TYPES                   Kotlin  GEN     Java   LOAD     Kotlin                    Any          Object     ...
CLASSES         open class Parent(p : Bar) {             open fun foo() {             }                    fun bar() {    ...
TRAITS         trait T1 : Class1, OtherTrait {             // no state         }         class Foo(p : Bar) : Class1(p), T...
DISAMBIGUATION         trait A {             fun foo() : Int = 1 // open by default         }         open class B() {    ...
FIRST-CLASS FUNCTIONS         fun foo(arg : String) : Boolean // function         (p : Int) -> Int // function type       ...
HIGH-ORDER FUNS         fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T>         filter(list, { s -> s.length...
HIGH-ORDER FUNS         fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T>         filter(list, { s -> s.length...
HIGH-ORDER FUNS         fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T>         filter(list, { s -> s.length...
HIGH-ORDER FUNS         fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T>         filter(list, { s -> s.length...
LOCAL FUNCTIONS         fun reachable(from : Vertex, to : Vertex) : Boolean {             val visited = HashSet<Vertex>() ...
INFIX FUNCTION CALLS         // regular call:         a.contains("123")         // infix call:         a contains "123"   ...
INFIX FUNCTION CALLS         // regular call:         a.contains("123")         // infix call:         a contains "123"   ...
LOCK EXAMPLE         myLock.lock()         try {             // do something         } finally {             myLock.unlock...
LOCK EXAMPLE         myLock.lock()         lock(myLock) {         try {                     // do something             //...
LOCK EXAMPLE         myLock.lock()                     lock(myLock) {         try {                                 // do ...
GENERICS: INVARIANCE         class List<T> {             fun add(t : T)             fun get(idx : Int) : T         }      ...
DECLARATION-SITE VARIANCE         class List<T> {              val intList = List<Int>()             fun add(t : T)       ...
DECLARATION-SITE VARIANCE         class List<T> {              val intList = List<Int>()             fun add(t : T)       ...
DECLARATION-SITE VARIANCE         class List<T> {              val intList = List<Int>()             fun add(t : T)       ...
USE-SITE VARIANCE         val intList = List<Int>()         val anyListOut : List<out Any> = intList         anyListOut.ad...
REIFIED GENERICS         // Type information is retained in runtime         foo is List<T>         Array<T>(10)         T....
REIFIED GENERICS         // Type information is retained in runtime         foo is List<T>         Array<T>(10)         T....
TUPLES         class Tuple2<out T1, out T2>(           val _1 : T1,           val _2 : T2         )         val pair : #(I...
PATTERN MATCHING 1/2         when (a) {             is Tree#(*, null) -> print("no right child")             is Tree#(val ...
PATTERN MATCHING 1/2         when (a) {             is Tree#(*, null) -> print("no right child")             is Tree#(val ...
PATTERN MATCHING 1/2         when (a) {             is Tree#(*, null) -> print("no right child")             is Tree#(val ...
PATTERN MATCHING 2/2         when (d) {             is mmddyy#(02, 16, val a) -> print("Feb 16th of $a"))         }       ...
CLASS OBJECTS 1/2         class Example() {             class object {                 fun create() = Example()           ...
CLASS OBJECTS 2/2         trait Factory<T> {             fun create() : T         }         class Example2() {            ...
RESOURCES         Documentation: http://jetbrains.com/kotlin         Blog: http://blog.jetbrains.com/kotlin               ...
THANKS         This presentation based on slides and speeches of         Andrey Breslav, author of Kotlin language.       ...
Q&A                             Sergey Lukjanov, 2012                            slukjanov@mirantis.com                   ...
Upcoming SlideShare
Loading in...5
×

Kotlin techtalk

1,221

Published on

Slides for internal techtalk

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,221
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Kotlin techtalk"

  1. 1. THE KOTLIN PROGRAMMING LANGUAGE Sergey Lukjanov, 2012 slukjanov@mirantis.com 1Thursday, 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 14 2Thursday, February 16, 12
  3. 3. OUTLINE •Motivation •Design goals •Feature overview •Basic syntax •Classes, types, inheritance •High-order functions •Generics •Tuples •Pattern matching •Class objects 3Thursday, February 16, 12
  4. 4. MOTIVATION 4Thursday, 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 GOALS 5Thursday, 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 fields) •Reified 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 EXAMPLES 8Thursday, February 16, 12
  17. 17. HELLO, WORLD! fun main(args : Array<String>) : Unit { println("Hello, World!"); } fun println(any : Any?) /* : Unit */ { System.out?.println(any); } 9Thursday, February 16, 12
  18. 18. HELLO, <NAMES>! fun main(args : Array<String>) { var names = ""; // names : String for(idx in args.indices) { names += args[idx] if(idx + 1 < args.size) { names += ", " } } println("Hello, $names!") // Groovy-style templates } val Array<*>.indices : Iterable<Int> get() = IntRange(0, size - 1) 10Thursday, February 16, 12
  19. 19. HELLO, <NAMES>! (FASTER) fun main(args : Array<String>) { var names = StringBuilder(); // names : StringBuilder for(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, <NAMES>! (REALISTIC) fun main(args : Array<String>) { println("Hello, ${args.join(", ")}!") } 12Thursday, February 16, 12
  21. 21. HELLO, <NAMES>! (REALISTIC) fun main(args : Array<String>) { println("Hello, ${args.join(", ")}!") } fun <T> Iterable<T>.join(separator : String) : String { val names = StringBuilder() forit (this) { names += it.next() if (it.hasNext) names += separator } return names.toString() ?: "" } fun <T> forit(col : Iterable<T>, f : (Iterator<T>) -> 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<String>) { val x = parseInt("1027") val y = parseInt("Hello, World!") // y == null println(x?.times(2)) // can’t 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<String>) { 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 0 is Int -> obj else -> -1 } } 16Thursday, February 16, 12
  28. 28. TYPES 1/2 Syntax Class types List<Foo> Nullable types Foo? Function types (Int) -> String Tuple types (Int, Int) Self types This 17Thursday, February 16, 12
  29. 29. TYPES 2/2 Special types Top Any? Bottom Nothing No meaningful return value Unit 18Thursday, February 16, 12
  30. 30. TYPES HIERARCHY Any? Int? String? ... Any Nothing? Int Unit Nothing Complete lattice 19Thursday, February 16, 12
  31. 31. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  32. 32. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  33. 33. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  34. 34. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  35. 35. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  36. 36. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  37. 37. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  38. 38. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  39. 39. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  40. 40. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 20Thursday, February 16, 12
  41. 41. MAPPING TO JAVA TYPES Kotlin GEN Java LOAD Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Array<Foo> Foo[] Array<Foo?>? Array<Int> int[] Array<Int>? List<Int> List<Integer> List<Int?>? Nothing - - Foo Foo Foo? 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 annotations 21Thursday, 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<A>.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 <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T> filter(list, { s -> s.length < 3 }) 25Thursday, February 16, 12
  47. 47. HIGH-ORDER FUNS fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T> filter(list, { s -> s.length < 3 }) filter(list) { s -> s.length < 3 } 25Thursday, February 16, 12
  48. 48. HIGH-ORDER FUNS fun <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T> 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 <T> filter( c : Iterable<T>, f: (T)->Boolean):Iterable<T> filter(list, { s -> s.length < 3 }) filter(list) { s -> s.length < 3 } // if only one arg: filter(list) { it.length < 3 } 25Thursday, February 16, 12
  50. 50. LOCAL FUNCTIONS fun reachable(from : Vertex, to : Vertex) : Boolean { val visited = HashSet<Vertex>() fun dfs(current : Vertex) { // here we return from the outer function: if (current == to) return@reachable true // And here - from local function: if (!visited.add(current)) return for (v in current.neighbors) dfs(v) } dfs(from) return false // if dfs() did not return true already } 26Thursday, February 16, 12
  51. 51. INFIX FUNCTION CALLS // regular call: a.contains("123") // infix call: a contains "123" 27Thursday, February 16, 12
  52. 52. INFIX FUNCTION CALLS // regular call: a.contains("123") // infix call: a contains "123" // “LINQ” users .filter { it hasPrivilege WRITE } .map { it -> it.fullName } .orderBy { it.lastName } 27Thursday, February 16, 12
  53. 53. LOCK EXAMPLE myLock.lock() try { // do something } finally { myLock.unlock() } 28Thursday, February 16, 12
  54. 54. LOCK EXAMPLE myLock.lock() lock(myLock) { try { // do something // do something } } finally { myLock.unlock() } 28Thursday, February 16, 12
  55. 55. LOCK EXAMPLE myLock.lock() lock(myLock) { try { // do something // do something } } finally { myLock.unlock() } inline fun <T> lock(l : Lock, body : () -> T) : T { l.lock() try { return body() } finally { l.unlock() } } 28Thursday, February 16, 12
  56. 56. GENERICS: INVARIANCE class List<T> { fun add(t : T) fun get(idx : Int) : T } val intList = List<Int>() // We should not be able to do it: val anyList : List<Any> = intList anyList.add("1") // Cause of the problem val i : Int = intList.get(0) // !!! 29Thursday, February 16, 12
  57. 57. DECLARATION-SITE VARIANCE class List<T> { val intList = List<Int>() fun add(t : T) val anyList : List<Any> = intList fun get(idx : Int) : T } 30Thursday, February 16, 12
  58. 58. DECLARATION-SITE VARIANCE class List<T> { val intList = List<Int>() fun add(t : T) val anyList : List<Any> = intList fun get(idx : Int) : T } class Producer<out T> { val intProd = Producer<Int>() fun get() : T val anyProd : Producer<Any> = intProd } 30Thursday, February 16, 12
  59. 59. DECLARATION-SITE VARIANCE class List<T> { val intList = List<Int>() fun add(t : T) val anyList : List<Any> = intList fun get(idx : Int) : T } class Producer<out T> { val intProd = Producer<Int>() fun get() : T val anyProd : Producer<Any> = intProd } class Consumer<in T> { val anyCons = Consumer<Any>() fun add(t : T) val intCons : Consumer<Int> = anyCons } 30Thursday, February 16, 12
  60. 60. USE-SITE VARIANCE val intList = List<Int>() val anyListOut : List<out Any> = intList anyListOut.add("1") // Not available val i : Int = intList.get(0) // No problem val anyList = List<Any>() val intListIn : List<in Int> = anyList intListIn.add(123) val obj = intListIn.get(0) // : Any? 31Thursday, February 16, 12
  61. 61. REIFIED GENERICS // Type information is retained in runtime foo is List<T> Array<T>(10) T.create() T.javaClass 32Thursday, February 16, 12
  62. 62. REIFIED GENERICS // Type information is retained in runtime foo is List<T> Array<T>(10) T.create() T.javaClass // Java types is still erased... foo is java.util.List<*> 32Thursday, February 16, 12
  63. 63. TUPLES class Tuple2<out T1, out T2>( val _1 : T1, val _2 : T2 ) val pair : #(Int, String) = #(1, "") // same as Tuple2<Int, String>(1, "") when (x) { is #(null, *) => throw NullPointerException() is #(val a, val b) => print(a, b) } print("left = ${pair._1}, right = ${pair._2}") val point : #(x : Int, y : Int) // labeled tuple print("x = ${point.x}, y = ${point.y}") val point : #(x : Int, y : Int) = #(y = 10, x = 5) 33Thursday, February 16, 12
  64. 64. PATTERN MATCHING 1/2 when (a) { is Tree#(*, null) -> print("no right child") is Tree#(val l is Tree, val r is Tree) -> print("$l and $r") is Tree -> print("just a tree") is #(*, val b in 1..100) -> print(b) else -> print("unknown") } 34Thursday, February 16, 12
  65. 65. PATTERN MATCHING 1/2 when (a) { is Tree#(*, null) -> print("no right child") is Tree#(val l is Tree, val r is Tree) -> print("$l and $r") is Tree -> print("just a tree") is #(*, val b in 1..100) -> print(b) else -> print("unknown") } class Tree(val left : Tree?, val right : Tree?) 34Thursday, February 16, 12
  66. 66. PATTERN MATCHING 1/2 when (a) { is Tree#(*, null) -> print("no right child") is Tree#(val l is Tree, val r is Tree) -> print("$l and $r") is Tree -> print("just a tree") is #(*, val b in 1..100) -> print(b) else -> print("unknown") } class Tree(val left : Tree?, val right : Tree?) decomposer fun Any?.Tree() : #(Tree?, Tree?)? { return if (this is Tree) #(this.left, this.right) else null } 34Thursday, February 16, 12
  67. 67. PATTERN MATCHING 2/2 when (d) { is mmddyy#(02, 16, val a) -> print("Feb 16th of $a")) } class Date(val timestamp : Long) { fun mmddyy() : #(Int, Int, Int)? = #(month, day, year) } fun Date.mmddyy() : #(Int, Int, Int)? = #(month, day, year) 35Thursday, February 16, 12
  68. 68. CLASS OBJECTS 1/2 class Example() { class object { fun create() = Example() } } val e = Example.create() 36Thursday, February 16, 12
  69. 69. CLASS OBJECTS 2/2 trait Factory<T> { fun create() : T } class Example2() { class object : Factory<Example2> { override fun create() = Example2() } } val factory : Factory<Example2> = Example2 val e2 : Example2 = factory.create() 37Thursday, February 16, 12
  70. 70. RESOURCES Documentation: http://jetbrains.com/kotlin Blog: http://blog.jetbrains.com/kotlin 38Thursday, February 16, 12
  71. 71. THANKS This presentation based on slides and speeches of Andrey Breslav, author of Kotlin language. XThursday, February 16, 12
  72. 72. Q&A Sergey Lukjanov, 2012 slukjanov@mirantis.com 39Thursday, February 16, 12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×