kotlin: foss' a vota bona (could be the right time)

34
Kotlin foss' a vota bona (could be the right time)

Upload: davide-cerbo

Post on 21-Jan-2018

34 views

Category:

Software


2 download

TRANSCRIPT

Kotlinfoss' a vota bona

(could be the right time)

DisclaimerLa presentazione contiene battute stupide e un

sacco di println

Chi sono

Mi chiamo Davide Cerbo e scrivo codice brutto da quando avevo 7

anni, dal 2003 mi pagano per farlo.

C’era una volta OAK

Correva l’anno 1992 ed iniziava Tangentopoli

C’era una volta OAK Java

Nasce nel 1995, in America ha la patente e da 12 mesi può bere

alcolici.

Il grande problema

Backward compatibility (L’arte di rompere la tua applicazione perché le altre evolvono e la tua non può.)

JDK 1.0 (21 gennaio 1996)JDK 1.1 (19 febbraio 1997)J2SE 1.2 (8 dicembre 1998)J2SE 1.3 (8 maggio 2000)J2SE 1.4 (6 febbraio 2002)

J2SE 5.0 (30 settembre 2004)Java SE 6 (11 dicembre 2006)

Java SE 7 (28 luglio 2011)Java SE 8 (18 marzo 2014)

Tutte compatibili!

JVM != Java

JAVA JVM

https://dzone.com/articles/why-learn-kotlin-infographic

Dal 2010(2012) ad oggi: KOTLIN!

Nella botte piccola c’è il vino buono

From Java to Kotlinhttps://github.com/MindorksOpenSource/from-java-to-kotlin

Var o Val

var serie = “A”

var a = “Benevento in serie $serie”

val a = “Benevento in serie $serie”

Fun

fun main(args: Array<String>){

hello(“Davide, “Salerno”)

}

fun hello(name: String, city: String){

println(“Ciao $name da $city”)

}

Fun fun functions

fun hello(name: String, city : String = "Salerno") = println("Ciao $name da $city")

hello(name=”Davide”)

hello(city=”Salerno”, name=”Valentina”)

Int.multiply(x:int) = this * 5

infix Int.multiply(x:int) = this * 5

tailrec fun factorial(accumulator: Int, n: Int): Int = if (n == 1) accumulator else factorial(accumulator * n, n - 1)

Nella vita ci vuole Class?

open class Person(val name: String) {

init { println(“init…”) }

open fun speak() { println("Hi $name!") }

infix fun and(o: Person) = "Hi ${o.name} & ${this.name}"

}

Person(“Davide”) and Person(“Valentina”)

Equals, hashCode, toString e copy, mai più!

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

val davide = User(“Davide”, 35)val davideJunior = davide.copy(age=0)

val (name, age) = davide println("$name $age years old")

Lambda

arrayOf("ciao", "davide").forEach { println("Hello $it!") }

val doubled = ints.map { value -> value * 2 }

val doubled = ints.map { it * 2 }

class Customer(name: String) : Person(name) { fun forEach(action: (line: Char) -> Unit) = name.forEach(action) fun hello(action: (line: String) -> String) = "Hello ${action.invoke(name)}" }

Tu dimmi quando, quando...

fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" }

describe(Person(“davide”))

Null è sicuro

var test:String = "ciao" test = null

var test:String? = "ciao" test = null

println(test?.length)

println(test!!.length)

val davide = User(test, 35)

val davide = User(test ?: "Mino", 35)

?:Elvis operator

A questo punto mi devo ricordare di dire che è bello perché è semplice.

Scriviamo meno e con maggiore significato.

04 Gennaio 2017Introducing Kotlin support in Spring Framework 5.0

arrayOf("ciao", "davide").forEach { println("Hello $it!") }

Deploy con Apache Tomcat

git clone [email protected]:JetBrains/kotlin-examples.git

cd kotlin-examples/tutorials/servlet-web-applications/

gradle war

curl -O http://apache.panu.it/tomcat/tomcat-9/v9.0.0.M21/bin/apache-tomcat-9.0.0.M21.zip

unzip apache-tomcat-9.0.0.M21.zip

cp build/servlet-web-applications.war apache-tomcat-9.0.0.M21/webapp

cd apache-tomcat-9.0.0.M21/bin

./startup.sh

http://localhost:8080/servlet-web-applications/hello

Java + Kotlin (+ Maven)

Cosa ci siamo persi?

● Spring WebFlux● Reactor● Coroutine● Gradle + Kotlin● Android + Kotlin● Javascript + Kotlin● Native + Kotlin● Spring 5 + Kotlin● JUnit + Kotlin

Domande