-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Ceylon Kotlin ( 1)

, 07 2017 . 12:25 +

Kotlin. , ? , Kotlin. : , Ceylon , Kotlin, Java. Ceylon - , .


, Ceylon, , . Java ( , 10 , Java 1.4 Java 8), Java . Scala, Java . eylon, Ceylon , . , . , Java, Java , .


Ceylon Red Hat, Gavin King, Hibernate. , Java, , , , . . 1.3.2, , 1.2.0.


Ceylon JavaScript, JVM.


, , Ceylon ( Kotlin ):


0# Java


, Kotlin, Scala, Ceylon 100 % Java. Java-, Ceylon. Java- , , , Ceylon Java. Java Ceylon , Java .


1#


Ceylon . Java , Ceylon . , Scala Kotlin Java. , , Kotlin:


class Foo(String a) {

    String b= "b";           //  unmodifiable
    variable Integer i = 0;  // variable means modifiable

    void hello() {
        value str = "Hello";
        print("``str`` World");
    }

    Integer sum(Integer x, Integer y) {
        return x + y;
    }

    Float maxOf(Float a, Float b) => if (a > b) then a else b
}

Java Ceylon.


2#


String.format() Java, :


value x = 4;
value y = 7;
print("sum of ``x`` and ``y`` is ``x + y``") ; // sum of 4 and 7 is 11

, Kotlin, Java .


3#


Ceylon , , :


value a = "abc";                         // type inferred to String
value b = 4;                             // type inferred to Integer

Float c = 0.7;                   // type declared explicitly
List d = ArrayList();     // type declared explicitly

4# (Smart Casts)


Ceylon , . . instanceof :


if (is String obj) {
    print(obj.uppercased)     // obj is now known to be a String
}

5# (Intuitive Equals)


equals(), == :


value john1 = Person("John"); //we override equals in Person
value john2 = Person("John");
print(john1 == john2);    // true  (structural equality)
print(john1 === john2);    // false  (referential equality)

6#


:


void build(String title, Integer width = 800, Integer height = 600) {
    return Frame(title, width, height);
}

7#


:


build("PacMan", 400, 300)                           // equivalent
build {title = "PacMan"; width = 400; height = 300;}  // equivalent
build {title = "PacMan"; height = 300;}  // equivalent with default width

8# switch


switch:


switch (obj)
case(1)                              { print("x is 1"); }
case(2)                              { print("x is 2"); }
case(3 | 4)                          { print("x is 3 or 4"); }
case(is String)                      { print ("x is String"); }
case([Integer a, Float b, String c]) {print ("x is tuple with Integer ``a``, Float ``b`` and String ``c``");}
else                                 { print("x is out of range");}

switch , switch :


Boolean|IllegalStateException res = 
    switch(obj)
    case(null) false
    case(is String) true
    else IllegalStateException();

pattern matching, .


Kotlin Ceylon , switch disjoint, , switch . , if.


9#


set & get, . . .


class Frame() {
    variable Integer width = 800;
    variable Integer height = 600;

    Integer pixels => width * height;
}

10# Data Class


. , toString(), equals(), hashCode() copy(), , Java, 100 .


, , . , , :


class Person(shared String name, 
             shared String email,
             shared Integer age) extends DataObject() {}

value john = Person("John", "john@gmail.com", 112);
value johnAfterBirhstday = john.copy({`Person.age`->113;});
assertEquals(john, john.copy());
assertEquals(john.hash, john.copy().hash);

toString, , . , , . = , . , . , .


11# (Operator Overloading)


, :


class Vec(shared Float x, shared Float y) satisfies Summable {
    shared actual Vec plus(Vec v)  => Vec(x + v.x, y + v.y);
}

value v = Vec(2.0, 3.0) + Vec(4.0, 1.0);

12# (Destructuring Declarations)


, , , map:


for ([key -> [val1, val2, val3]] in map) {
    print("Key: ``key``");
    print("Value: ``val1``, ``val2``, ``val3``");
}

13# (Ranges)


:


for (i in 1..100) { ... } 
for (i in 0 : 100) { ... }
for (i in (2..10).by(2)) { ... } 
for (i in 10..2) { ... }  
if (x in 1..10) { ... }

Kotlin downTo.


14# - (Extension Functions)


. , . top level . , , String sayHello, "world".sayHello() sayHello("world"). .


, , IDE, .


15# Null


Java . String String null. , , Java- NPE.


Ceylon , null. null, , ?:


variable String a = "abc";
a = null;                // compile error

variable String? b = "xyz";
b = null;                // no problem

union types String? String|Null. :


variable String|Null  = "xyz";
 = null;                // no problem

Ceylon NPE, , null:


value x = b.length        // compile error: b might be null

, , . , , null, :


if (!exists b) { return; }
value x = b.length        // no problem

?., null NPE:


value x = b?.length;       // type of x is nullable Int

, --null, . null-, elvis- else


value name = ship?.captain?.name else "unknown";

NPE, :


value x = b?.length else NullPointerException()  // same as below
assert(!NullPointerException x);

16#


. :


value sum = (Integer x, Integer y) => x + y;  // type: Integer(Integer, Integer)
value res = sum(4,7)                      // res == 11

:


numbers.filter( (x) => x.isPrime() );
numbers.filter(isPrime)

:


    persons
        .filter ( (it) => it.age >= 18)
        .sort(byIncreasing(Person.name))
        .map ( Person.email )
        .each ( print );

, Ceylon DSL. DSL, Anko, Ceylon:


 VerticalLayout {
    padding = dip(30);  {
    editText {
        hint = "Name";
        textSize = 24.0;
    },
    editText {
        hint = "Password";
        textSize = 24.0;
    },
    button {
        "Login";
        textSize = 45.0;
    } }
};

17# IDE


, . eclipse , IDEA . , , Scala Kotlin. , IDE .


, Kotlin, Ceylon Kotlin DataObject ( ). , .


, Kotlin, Ceylon Android.


, ? Kotlin, 1 1.


, Ceylon , Kotlin, Scala, , . union types, intersection types, enumerated types, generics, , herd, , , for comprehensions. , , . .


Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330412/

:  

: [1] []
 

:
: 

: ( )

:

  URL