Slide 1

Slide 1 text

Kotlin & Ceylon A Sneak Peek Edoardo Vacchi  @evacchi © D O R O Z H K I N A flic.kr/p/hx59k9

Slide 2

Slide 2 text

New Languages on the JVM ? ▶ Do we need them ? ▶ Java must be backwards-compatible ▶ This limits its evolution ▶ How about starting over ?

Slide 3

Slide 3 text

Kotlin ▶ Developed by JetBrains ▶ (IntelliJ IDEA, PhpStorm, RubyMine…) ▶ Developed with enterprise in mind ▶ A better Java ▶ It takes hint from Scala, C#, Groovy ▶ (some Groovy devs are involved) ▶ Strictly interoperable with Java Milestone 11 (M12 is coming)

Slide 4

Slide 4 text

Ceylon ▶ Red Hat / JBoss ▶ Project Lead: Gavin King ▶ (Hybernate, Seam, …) ▶ “Reimagining” Java ▶ Interesting type system ▶ Eclipse IDE Release 1.1 (1.2 is coming)

Slide 5

Slide 5 text

Things in common: ▶ Statically, strongly typed ▶ Less code for tedious tasks ▶ Type inference ▶ Nullable and non-nullable types ▶ Higher-order function literals ▶ First-class singleton object (à la Scala) ▶ Properties ▶ “Data” classes ▶ Operator overloading ▶ Focus on Enterprise Support

Slide 6

Slide 6 text

Type Inference val aNumber = 10 // Kotlin value aNumber = 10; // Ceylon final int aNumber = 10; // Java

Slide 7

Slide 7 text

Higher-order functions List(1, 2, 3) map { x -> x + 1 } value list = ArrayList { 1, 2 }.map( (x) => x+1 );

Slide 8

Slide 8 text

Nullable Types val name: String? = getNullableString() // Kotlin val sub = name?.subSequence(0,2) // safe navigation operator String? name = getNullableString() ; // Ceylon if (exists name) { value sub = name.subSequence(0,2); ... } else { ... }

Slide 9

Slide 9 text

Inheritance: Kotlin trait Animal { val name: String fun eat(Food food): String { return "nom nom" } } class Dog(val name: String) : Animal { fun bark() = "bark!" } object Lassie extends Dog("Lassie")

Slide 10

Slide 10 text

Inheritance: Ceylon interface Animal { shared formal String name; shared String eat(Food food) { return "nom nom"; } } class Dog(shared actual String name) satisfies Animal { shared String bark() => "bark!"; } object lassie extends Dog("Lassie") {}

Slide 11

Slide 11 text

Data Classes data class Person(val name: String, var address: String) val myFoo = Foo("John Doe", "was here") // no `new` here val theName = myFoo.name // getName() auto-gen for Java myFoo.address = "was there" // setAddress(String) class Polar(name, address) { shared String name; // getName$() shared variable String address; // get/setAddress$() }

Slide 12

Slide 12 text

when Statement (Kotlin) trait BinTree class Node(val value: T, val left: BinTree, val right: BinTree): BinTree class Leaf(val value: T) : Node(value) ... fun depth(t: BinTree): Int = when (t) { is Leaf -> 1 is Node -> max(path(t.left), path(t.right)) else -> throw UnsupportedOperationException() // no ADTs, must manage else case }

Slide 13

Slide 13 text

switch with ADTs (Ceylon) abstract class Node() of Leaf | Branch {} ... Node node = ... ; switch (node) case (is Leaf) { ... } case (is Branch) { .... } // no default case

Slide 14

Slide 14 text

Operator Overloading: Kotlin by Convention Expression Translated to a + b a.plus(b) a - b a.minus(b) a[i] a.get(i) a[i] = b a.set(i, b) … …

Slide 15

Slide 15 text

Operator Overloading: Ceylon Through Interfaces ▶ Summable supports the infix + operator (plus(X) ), ▶ Comparable supports the comparison operators (compare(X) ), ▶ Correspondence supports the index operator, ▶ Ranged supports the segment and span operators ▶ …

Slide 16

Slide 16 text

What’s Different ?

Slide 17

Slide 17 text

Kotlin

Slide 18

Slide 18 text

Extension Functions package com.example fun String.reverse() { return StringBuilder(this).reverse().toString() } package com.example public final class ExamplePackage { public static String reverse(String self) { return StringBuilder(self).reverse().toString(); } }

Slide 19

Slide 19 text

Native Delegation trait Base { fun print() } class BaseImpl(val x : Int) : Base { override fun print() { print(x) } } class Derived(b : Base) : Base by b fun main() { val b = BaseImpl(10) Derived(b).print() // prints 10 }

Slide 20

Slide 20 text

Delegated Properties val foo: T by class User(val map: Map) { val name: String by Delegates.mapVal(map) val age: Int by Delegates.mapVal(map) } val lazy: String by Delegates.lazy { println("computed!") "Hello" }

Slide 21

Slide 21 text

Real Java Collections ▶ mutability and immutability tacked on top transparently object KMeans { fun clusters(xs: List, centroids: List) = (xs groupBy { x -> closest(x, centroids) }).values().toList() ... } // kotlin.List maps to a regular java.util.ArrayList ▶ compiled to efficient Java 6 bytecode

Slide 22

Slide 22 text

Great Interoperability ▶ properties become getters and setters ▶ extension functions become static methods ▶ singleton objects have their INSTANCE field ▶ nullable types are transparent and use annotations

Slide 23

Slide 23 text

Ceylon

Slide 24

Slide 24 text

Union and Intersection Types File|Path|SyntaxError parsePath(String path) => ... ... value result = parsePath(filePath); switch(result) case (is File|Path) { ... } case (is SyntaxError) { ... } Printable&Sized&Persistent printableSizedPersistent = ... ;

Slide 25

Slide 25 text

Nullable Types ▶ In Kotlin, a special case of the type system ▶ In Ceylon String? is a shorthand for String|Null Not a special case, defined within the language shared abstract class Null() of null extends Anything() {} shared object null extends Null() {} shared abstract class Anything() of Object | Null {}

Slide 26

Slide 26 text

Null Checking if (exists someNullableString) { ... } else { ... } ▶ Less convenient than Kotlin’s

Slide 27

Slide 27 text

Type Inference Q: What single type does this list contains? value list = ArrayList { 1, 2, 3.0, 4.0 } A: ArrayList

Slide 28

Slide 28 text

Metaprogramming

Slide 29

Slide 29 text

Roundup: Kotlin ▶ Overall, Kotlin feels like an incremental improvement over Java ▶ pretty true, you can basically mix them ▶ they will work almost seamlessly ▶ Most features are already in Java as “best practices” or idioms ▶ What Kotlin does is to raise them at the language level, with some sugar ▶ Developers listen to feedback a lot ▶ Compiles to Java 6 bytecode (works on Android)

Slide 30

Slide 30 text

Roundup: Ceylon ▶ “Reimagining Java” from scratch ▶ «more readable» identifiers shared variable value hello = "Hello"; class Dog satisfies Animal {} ▶ Interesting, elegant type system ▶ Strong metaprogramming ▶ Less interoperable ▶ (e.g., collections)

Slide 31

Slide 31 text

Conclusions ▶ Do They Stand a Chance ? ▶ Java 8 with a tad of Lombok @Data public class Person { private final String name, address; } @ExtensionMethod({java.util.Arrays.class, Extensions.class}) public class ExtensionMethodExample { public String test() { int[] intArray = {5, 3, 8, 2}; intArray.sort(); }} ▶ Doing away with backwards compatibility

Slide 32

Slide 32 text

No content