2014-11-01 01 Денис Нелюбин. О сортах кофе

36
Different grades of coffee

Upload: -

Post on 12-Jul-2015

59 views

Category:

Software


2 download

TRANSCRIPT

Page 1: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Different grades of coffee

Page 2: 2014-11-01 01 Денис Нелюбин. О сортах кофе
Page 3: 2014-11-01 01 Денис Нелюбин. О сортах кофе
Page 4: 2014-11-01 01 Денис Нелюбин. О сортах кофе
Page 5: 2014-11-01 01 Денис Нелюбин. О сортах кофе
Page 6: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Java (Android)

TextView textView = (TextView) findViewById(R.id.text);

textView.setText("Hello");

Page 7: 2014-11-01 01 Денис Нелюбин. О сортах кофе

A better Java?

View view = findViewById(R.id.text);

if (view instanceof TextView) {

TextView textView = (TextView) view;

textView.setText("Hello");

}

Page 8: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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")

Page 9: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Ceylon

value view = findViewById(R.id.text);

if (is TextView view) {

view.setText("Hello");

}

Page 10: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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()

Page 11: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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;

Page 12: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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);

Page 13: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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)

Page 14: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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);

Page 15: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Properties (Kotlin)

class Address(var street : String, var building : String) { public var asString : String get() { return "$street, $building" } set(value) { //parse value }}

Page 16: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Attributes (Ceylon)

class Address(street, building) { shared variable String street; shared variable String building; shared String asString => "``street``, ``building``"; assign asString { //parse value }}

Page 17: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Data classes (Kotlin)

data class User(val name: String, val age: Int)

Page 18: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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();

Page 19: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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();

Page 20: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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();

Page 21: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Type inference

val i = 123

public val j : Int = 123

Page 22: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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() }}

Page 23: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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(); }}

Page 24: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Operators (Kotlin)

class Sum(var value : Int) { public fun plus(other : Int) { value += other }} val s = Sum(42)s + 1print(s.value)

Page 25: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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);

Page 26: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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 }

Page 27: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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);

Page 28: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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")}

Page 29: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Modularity (Java)

Page 30: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Modularity (Ceylon)

Page 31: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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 } }}

Page 32: 2014-11-01 01 Денис Нелюбин. О сортах кофе

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; } }};

Page 33: 2014-11-01 01 Денис Нелюбин. О сортах кофе

1995 2012 2011 2003

Page 35: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Thanks

Denis Nelubinhttp://google.com/+DenisNelubin

Page 36: 2014-11-01 01 Денис Нелюбин. О сортах кофе

Type hierarchy (Java)