PROJECT KOTLIN

http://kotlinlang.org

from Jetbrains, Inc.

Java.next?

  • Statically Typed
  • Targets both Java and JavaScript
  • Free-standing Functions
  • String interpolation
  • Extension Methods
  • Lambdas
  • Infix calls
  • Null-safe typing
  • First-class IDE support
  • Maven, Gradle, Ant support.
  • ...and all without the batshit ==>>

A Quick Language Tour

package level functions

package com.talios.kotlin

fun sayHello(name: String) {
  println("Hello " + name)
}

String interpolation

package com.talios.kotlin

fun sayHello(name: String) {
  println("Hello $name")
}

Extension Methods

package com.talios.kotlin

fun String.wrapDiv(cssClass: String = "") = "
$this
" fun sayHello(name: String) = println("Hello $name".wrapDiv()) fun shoutHello(name: String) = println( "hello $name".wrapDiv(cssClass="loud")

Lambdas

fun lock<T>(lock : Lock, body : () -> T) : T {
  lock.lock()
  try {
    return body()
  }
  finally {
    lock.unlock();
  }
}
fun toBeSynchronized() = sharedResource.operation()
val result = lock(lock, ::toBeSynchronized)
val result = lock(lock, { sharedResource.operation() })
lock (lock) {
  sharedResource.operation()
}

INfix notation

// Define extension to Int
fun Int.shl(x: Int) {
..
}

// call extension function using infix notation

1 shl 2

// is the same as

1.shl(2)


data classes

 data class Person(first: String, last: String, age: Integer)
  • Immutable
  • Equals
  • Hash Codes
  • To String
  • Copying
  • Desctrucuturing

Destructuring binds

data class Person(first: String, last: String, age: Integer)
val bob = Person("Bob", "Dobalina", 37)
val (first, last, age) = bob
class Person {
  fun component1(): String =...
  fun component2(): String =...
  fun component3(): Integer =...
}

PROJECT KOTLIN

By talios

PROJECT KOTLIN

  • 1,290