2014-11-01 01 Денис Нелюбин. О сортах кофе
TRANSCRIPT
Different grades of coffee
Java (Android)
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Hello");
A better Java?
View view = findViewById(R.id.text);
if (view instanceof TextView) {
TextView textView = (TextView) view;
textView.setText("Hello");
}
Kotlin
val view = findViewById(R.id.text)
if (view is TextView) {
view.setText("Hello")
}
val textView = findViewById(R.id.text) as? TextView
textView?.setText("Hello")
Ceylon
value view = findViewById(R.id.text);
if (is TextView view) {
view.setText("Hello");
}
Null safety (Kotlin)
var a : String = "abc"a = null // compilation error
var b : String? = "abc"b = null // ok
val l1 = if (b != null) b.length() else -1val l2 = b?.length() ?: -1
val l3 = b!!.length()
Null safety (Ceylon)
variable String a = "abc";a = null; // compilation error
variable String? b = "abc";b = null; // ok
Integer l1; value b1 = b;if (exists b1) { l1 = b1.size; } else { l1 = -1; }
value l2 = b?.size else -1;
Union types (Ceylon)
String? str1 = null;String|Null str2 = null;
void printType(String|Integer val) { switch (val) case (is String) { print("String: ``val``"); } case (is Integer) { print("Integer: ``val``"); }}
printType("hello");printType(69);
Immutability (Kotlin)
val s1 = "abc"s1 = "def" //compilation error var s2 = "abc"s2 = "def"
val l = listOf(1, 2, 3)val m = arrayListOf<Int>()m.addAll(l)
Immutability (Ceylon)
value s1 = "abc";s1 = "def"; //compilation error
variable value s2 = "abc";s2 = "def";
value l = [1, 2, 3];value m = ArrayList();m.addAll(l);
Properties (Kotlin)
class Address(var street : String, var building : String) { public var asString : String get() { return "$street, $building" } set(value) { //parse value }}
Attributes (Ceylon)
class Address(street, building) { shared variable String street; shared variable String building; shared String asString => "``street``, ``building``"; assign asString { //parse value }}
Data classes (Kotlin)
data class User(val name: String, val age: Int)
Generics (Java)
class Box<T> { private T item; public Box(T item) { this.item = item; } public T get() { return item; }}Box<Integer> intBox = new Box<Integer>(123);Box<? extends Number> numBox = intBox;Number num = numBox.get();
Generics (Kotlin)
class Box<out T>(val item : T) { public fun get() : T { return item; }}
val intBox = Box<Int>(123);val numBox : Box<Number> = intBox;val num : Number = numBox.get();
Generics (Ceylon)
class Box<out Item>(Item item) { shared Item get() { return item; }}
Box<Integer> intBox = Box(123);Box<Number<Integer>> numBox = intBox;Number<Integer> num = numBox.get();
Type inference
val i = 123
public val j : Int = 123
Traits (Kotlin)
trait A { fun foo() { print("A") }}trait B { fun foo() { print("B") }}class C() : A, B { override fun foo() { super<A>.foo() super<B>.foo() }}
Interfaces (Ceylon)interface Foo { shared formal void foo();}interface A satisfies Foo { shared actual default void foo() => print("A");}interface B satisfies Foo { shared actual default void foo() => print("B");}class C() satisfies A & B { shared actual void foo() { (super of A).foo(); (super of B).foo(); }}
Operators (Kotlin)
class Sum(var value : Int) { public fun plus(other : Int) { value += other }} val s = Sum(42)s + 1print(s.value)
Operators (Ceylon)
class Sum(val) satisfies Summable<Sum> { shared Integer val; shared actual Sum plus(Sum other) { return Sum(this.val + other.val); }}
variable value s = Sum(42);value one = Sum(1);s = s + one;print(s.val);
Functions (Kotlin)
fun <T, R> List<T>.map(transform : (T) -> R) : List<R> { val result = arrayListOf<R>() for (item in this) result.add(transform(item)) return result} listOf(1, 2, 3).map { it -> it * 2 }
Functions (Ceylon)
List<Result> map<Item, Result> (List<Item> list)(Result(Item) transform) { List<Result> result = ArrayList(); for (item in list) { result.add(transform(item)); } return result;}
map<Integer, Integer>(Array{1, 2, 3}) ((item) => item * 2);
When expression (Kotlin)
when (x) { parseInt(s) -> print("s encodes x") else -> print("s does not encode x")}
when (x) { in 1..10 -> print("x is in the range") !in 10..20 -> print("x is outside the range") else -> print("none of the above")}
Modularity (Java)
Modularity (Ceylon)
DSL (Kotlin)
html { head { title {+"XML encoding with Kotlin"} } body { h1 {+"XML encoding with Kotlin"} p {+"this format can be used as an alternative markup to XML"} a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} p { +"This is some" b {+"mixed"} +"text. For more see the" a(href = "http://jetbrains.com/kotlin") {+"Kotlin"} +"project" } p {+"some text"}
// content generated by p { for (arg in args) +arg } }}
DSL (Ceylon)
Table table = Table { title = "Squares"; rows = 5; Border { padding = 2; weight = 1; }; Column { heading = "x"; width = 10; String content(Integer row) { return row.string; } }, Column { heading = "x^2"; width=10; String content(Integer row) { return (row^2).string; } }};
1995 2012 2011 2003
Demo
http://kotlin-demo.jetbrains.com/ http://try.ceylon-lang.org/
Thanks
Denis Nelubinhttp://google.com/+DenisNelubin
Type hierarchy (Java)