Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin & Ceylon — A Sneak Peek

Kotlin & Ceylon — A Sneak Peek

A sneak peek on Kotlin & Ceylon. 20-minutes version of my longer, more detailed talk on the same topic.

Edoardo Vacchi

April 18, 2015
Tweet

More Decks by Edoardo Vacchi

Other Decks in Programming

Transcript

  1. Kotlin & Ceylon A Sneak Peek Edoardo Vacchi  @evacchi

    © D O R O Z H K I N A flic.kr/p/hx59k9
  2. New Languages on the JVM ? ▶ Do we need

    them ? ▶ Java must be backwards-compatible ▶ This limits its evolution ▶ How about starting over ?
  3. 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)
  4. Ceylon ▶ Red Hat / JBoss ▶ Project Lead: Gavin

    King ▶ (Hybernate, Seam, …) ▶ “Reimagining” Java ▶ Interesting type system ▶ Eclipse IDE Release 1.1 (1.2 is coming)
  5. 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
  6. Type Inference val aNumber = 10 // Kotlin value aNumber

    = 10; // Ceylon final int aNumber = 10; // Java
  7. Higher-order functions List(1, 2, 3) map { x -> x

    + 1 } value list = ArrayList { 1, 2 }.map( (x) => x+1 );
  8. 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 { ... }
  9. 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")
  10. 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") {}
  11. 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$() }
  12. when Statement (Kotlin) trait BinTree<T> class Node<T>(val value: T, val

    left: BinTree<T>, val right: BinTree<T>): BinTree<T> class Leaf<T>(val value: T) : Node(value) ... fun depth<T>(t: BinTree<T>): Int = when (t) { is Leaf -> 1 is Node -> max(path(t.left), path(t.right)) else -> throw UnsupportedOperationException() // no ADTs, must manage else case }
  13. switch with ADTs (Ceylon) abstract class Node() of Leaf |

    Branch {} ... Node node = ... ; switch (node) case (is Leaf) { ... } case (is Branch) { .... } // no default case
  14. 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) … …
  15. Operator Overloading: Ceylon Through Interfaces ▶ Summable<X> supports the infix

    + operator (plus(X) ), ▶ Comparable<X> supports the comparison operators (compare(X) ), ▶ Correspondence supports the index operator, ▶ Ranged supports the segment and span operators ▶ …
  16. 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(); } }
  17. 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 }
  18. Delegated Properties val foo: T by <expr> class User(val map:

    Map<String, Any?>) { val name: String by Delegates.mapVal(map) val age: Int by Delegates.mapVal(map) } val lazy: String by Delegates.lazy { println("computed!") "Hello" }
  19. Real Java Collections ▶ mutability and immutability tacked on top

    transparently object KMeans { fun clusters(xs: List<Point>, centroids: List<Point>) = (xs groupBy { x -> closest(x, centroids) }).values().toList() ... } // kotlin.List maps to a regular java.util.ArrayList ▶ compiled to efficient Java 6 bytecode
  20. 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
  21. 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 = ... ;
  22. 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 {}
  23. Null Checking if (exists someNullableString) { ... } else {

    ... } ▶ Less convenient than Kotlin’s
  24. Type Inference Q: What single type does this list contains?

    value list = ArrayList { 1, 2, 3.0, 4.0 } A: ArrayList<Integer|Float>
  25. 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)
  26. 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)
  27. 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