Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- pragmatic, safe, concise, interoperable
- seamless interoperability from java -> kotlin, and kotlin -> java
- when calling java from kotlin, it will see @Nullable annotations and automatically make them nullable
- excellent IDE support - Kotlin is made by Jetbrains, the makers of IntelliJ which is the IDE behind Android Studio
- comes out of the box in Android Studio 3
- multiplatform vision
- - server side apps
- - android apps
- - transpile to JS
- - write Gradle build scripts
- - compile to swift coming soon
- you can invoke the REPL with `kotlinc`
- try.kotl.in for an online interactive shell
- --------------------------
- readonly vs mutable property by using val/var
- top level functions
- Semicolon is optional
- named parameters, default parameters
- omit the braces of a function if it's a single line
- fun max(a: Int, b: Int): Int = if (a > b) a else b
- for expressiona bodies, the return type of the function is optional
- fun max(a: Int, b: Int) = if (a > b) a else b
- type inference, first class functions
- String templating
- val name = "Elias"
- println("Hello, $name!")
- println("Hello, ${5 + 2}")
- properties instead of instance variables and getters/setters
- nullable types
- elvis operator, ?: corresponds to ?? in swift
- use the kotlin -> java converter, and also a java -> kotlin converter to help learning
- The code may not be the most idiomatic, but it works
- ranges:
- for (i in 1..10) println(i) // inclusive
- for (i in 1 until 10) println(i) // excluse
- hash map for key value in example
- iterate collection with index
- for ((key ,value) in arrayListOf("meow", "bork").withIndex()) println("$key $value")
- check if value is in a range with in
- if a in 0..10
- if a !in 0..10
- fun isLetter(c: Char) = c in 'a'..'z' || c in 'A'..'Z'
- "kotlin" in "java".."scala"
- kotlin treats checked and unchecked exceptions the same, and you don't have to makr the method that it throws (yay!)
- extension functions
- you have to explicitly export extension functions, this is to prevent accidental name collisions
- extension methods can't be overriden, because theu are declared externally to the view. The compile time type is used, not the runtime type.
- infix notation
- - member or extension functions
- - single param
- - marked with the infix keyword
- // Define extension to Int
- infix fun Int.shl(x: Int): Int {
- ...
- }
- // call extension function using infix notation
- 1 shl 2
- // is the same as
- 1.shl(2)
- tail recursion with the keyword `tailrec`
- */
- import kotlin.properties.Delegates
- //////////////////////////////////////////////////////////
- // data classes, smart casting, when
- //////////////////////////////////////////////////////////
- interface Expr
- data class Num(val value: Int): Expr
- data class Sum(val left: Expr, val right: Expr): Expr
- // smart cast
- fun eval(e: Expr): Int = when(e) {
- is Num -> e.value
- is Sum -> eval(e.left) + eval(e.right)
- else -> throw IllegalArgumentException("Invalid!")
- }
- val five = Num(5)
- val six = Num(6)
- val result = eval(Sum(left=five, right=six))
- println("The sum is $result")
- println("")
- /////////////////////////////////////////////////////////
- // class delegation
- // For when you have an object composed of simpler components,
- // but want the parent object to present the same interface
- /////////////////////////////////////////////////////////
- interface Component1 {
- fun someFunction1();
- }
- interface Component2 {
- fun someFunction2();
- }
- class Component1Impl(): Component1 {
- override fun someFunction1() {
- println("In function 1")
- }
- }
- class Component2Impl(): Component2 {
- override fun someFunction2() {
- println("IN function 2")
- }
- }
- class Message(c1: Component1, c2: Component2) : Component1 by c1, Component2 by c2
- val c1 = Component1Impl()
- val c2 = Component2Impl()
- val message = Message(c1, c2)
- message.someFunction1()
- message.someFunction2()
- ////////////////////////////////////////////////////
- // property observers delegate
- // map property delegate
- ////////////////////////////////////////////////////
- class User(val map: MutableMap<String, Any?>) {
- var name: String by map
- var age: Int by map
- var catchPhrase: String by Delegates.observable("initial") { prop, old, new ->
- println("$old -> $new")
- }
- }
- val user = User(mutableMapOf(
- "name" to "Elias",
- "age" to 27
- ))
- println("name: ${user.name}")
- println("age: ${user.age}")
- println("map: ${user.map}")
- user.name = "Elias Bagley"
- user.catchPhrase = "Howdy"
- println("name: ${user.name}")
- println("age: ${user.age}")
- println("map: ${user.map}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement