Advertisement
Guest User

Untitled

a guest
May 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.57 KB | None | 0 0
  1. /*
  2.  
  3. pragmatic, safe, concise, interoperable
  4.  
  5. seamless interoperability from java -> kotlin, and kotlin -> java
  6. when calling java from kotlin, it will see @Nullable annotations and automatically make them nullable
  7.  
  8. excellent IDE support - Kotlin is made by Jetbrains, the makers of IntelliJ which is the IDE behind Android Studio
  9. comes out of the box in Android Studio 3
  10.  
  11. multiplatform vision
  12. - server side apps
  13. - android apps
  14. - transpile to JS
  15. - write Gradle build scripts
  16. - compile to swift coming soon
  17.  
  18.  
  19. you can invoke the REPL with `kotlinc`
  20.  
  21. try.kotl.in for an online interactive shell
  22. --------------------------
  23.  
  24. readonly vs mutable property by using val/var
  25.  
  26. top level functions
  27.  
  28. Semicolon is optional
  29.  
  30. named parameters, default parameters
  31.  
  32. omit the braces of a function if it's a single line
  33. fun max(a: Int, b: Int): Int = if (a > b) a else b
  34.  
  35. for expressiona bodies, the return type of the function is optional
  36. fun max(a: Int, b: Int) = if (a > b) a else b
  37.  
  38.  
  39. type inference, first class functions
  40.  
  41. String templating
  42. val name = "Elias"
  43. println("Hello, $name!")
  44. println("Hello, ${5 + 2}")
  45.  
  46. properties instead of instance variables and getters/setters
  47.  
  48. nullable types
  49.  
  50. elvis operator, ?: corresponds to ?? in swift
  51.  
  52. use the kotlin -> java converter, and also a java -> kotlin converter to help learning
  53. The code may not be the most idiomatic, but it works
  54.  
  55. ranges:
  56. for (i in 1..10) println(i) // inclusive
  57. for (i in 1 until 10) println(i) // excluse
  58.  
  59. hash map for key value in example
  60.  
  61. iterate collection with index
  62. for ((key ,value) in arrayListOf("meow", "bork").withIndex()) println("$key $value")
  63.  
  64. check if value is in a range with in
  65. if a in 0..10
  66. if a !in 0..10
  67.  
  68. fun isLetter(c: Char) = c in 'a'..'z' || c in 'A'..'Z'
  69.  
  70. "kotlin" in "java".."scala"
  71.  
  72. kotlin treats checked and unchecked exceptions the same, and you don't have to makr the method that it throws (yay!)
  73.  
  74. extension functions
  75. you have to explicitly export extension functions, this is to prevent accidental name collisions
  76. extension methods can't be overriden, because theu are declared externally to the view. The compile time type is used, not the runtime type.
  77.  
  78. infix notation
  79. - member or extension functions
  80. - single param
  81. - marked with the infix keyword
  82. // Define extension to Int
  83. infix fun Int.shl(x: Int): Int {
  84. ...
  85. }
  86.  
  87. // call extension function using infix notation
  88.  
  89. 1 shl 2
  90.  
  91. // is the same as
  92.  
  93. 1.shl(2)
  94.  
  95. tail recursion with the keyword `tailrec`
  96. */
  97.  
  98. import kotlin.properties.Delegates
  99.  
  100. //////////////////////////////////////////////////////////
  101. // data classes, smart casting, when
  102. //////////////////////////////////////////////////////////
  103.  
  104. interface Expr
  105.  
  106. data class Num(val value: Int): Expr
  107.  
  108. data class Sum(val left: Expr, val right: Expr): Expr
  109.  
  110. // smart cast
  111. fun eval(e: Expr): Int = when(e) {
  112.   is Num -> e.value
  113.   is Sum -> eval(e.left) + eval(e.right)
  114.   else -> throw IllegalArgumentException("Invalid!")
  115. }
  116.  
  117. val five = Num(5)
  118. val six = Num(6)
  119. val result = eval(Sum(left=five, right=six))
  120. println("The sum is $result")
  121. println("")
  122.  
  123. /////////////////////////////////////////////////////////
  124. // class delegation
  125. // For when you have an object composed of simpler components,
  126. // but want the parent object to present the same interface
  127. /////////////////////////////////////////////////////////
  128.  
  129. interface Component1 {
  130.   fun someFunction1();
  131. }
  132.  
  133. interface Component2 {
  134.   fun someFunction2();
  135. }
  136.  
  137. class Component1Impl(): Component1 {
  138.   override fun someFunction1() {
  139.     println("In function 1")
  140.   }
  141. }
  142.  
  143. class Component2Impl(): Component2 {
  144.   override fun someFunction2() {
  145.     println("IN function 2")
  146.   }
  147. }
  148.  
  149. class Message(c1: Component1, c2: Component2) : Component1 by c1, Component2 by c2
  150.  
  151. val c1 = Component1Impl()
  152. val c2 = Component2Impl()
  153. val message = Message(c1, c2)
  154. message.someFunction1()
  155. message.someFunction2()
  156.  
  157.  
  158. ////////////////////////////////////////////////////
  159. // property observers delegate
  160. // map property delegate
  161. ////////////////////////////////////////////////////
  162.  
  163.  
  164. class User(val map: MutableMap<String, Any?>) {
  165.   var name: String by map
  166.   var age: Int by map
  167.  
  168.   var catchPhrase: String by Delegates.observable("initial") { prop, old, new ->
  169.     println("$old -> $new")
  170.   }
  171. }
  172.  
  173. val user = User(mutableMapOf(
  174.     "name" to "Elias",
  175.     "age" to 27
  176. ))
  177.  
  178. println("name: ${user.name}")
  179. println("age: ${user.age}")
  180. println("map: ${user.map}")
  181.  
  182.  
  183. user.name = "Elias Bagley"
  184. user.catchPhrase = "Howdy"
  185.  
  186.  
  187. println("name: ${user.name}")
  188. println("age: ${user.age}")
  189. println("map: ${user.map}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement