Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

To kotlin or not to kotlin. That's the question

125 views

Published on

The introduction of Kotlin

Published in: Software
  • Be the first to comment

To kotlin or not to kotlin. That's the question

  1. 1. To Kotlin or not to Kotlin, That is the question
  2. 2. 自我介紹 汪 永興 LINE 京都開發室 Freddie Wang
  3. 3. 1.5M 50+ 250+ Developers Team size Contributors
  4. 4. Kotlin is designed for modernized Java
  5. 5. • Readability (functional) • Reuse (extensible, DSL-friendly) • Interoperability with JVM • Safety tooling (except the doc generation tool...)
  6. 6. Standing on the shoulder of giants
  7. 7. But definitely not
  8. 8. The improvement(?) from Java: • Nullability • Lambda + inline • Properties • Extension • String template • Operator overloading • Data class • Inner function • No checked exception
  9. 9. val nullable: String? = "This is the wrong parent" val nonnuable: String = null //Compiler error val nullable: String? = null //OK Nullability
  10. 10. Lambda + Inline public inline fun measureTimeMillis(block: () -> Unit): Long { val start = System.currentTimeMillis() block() return System.currentTimeMillis() - start }
  11. 11. Lambda + Inline public inline fun measureTimeMillis(block: () -> Unit): Long { val start = System.currentTimeMillis() block() return System.currentTimeMillis() - start }
  12. 12. Lambda + Inline measureTimeMillis { val jobs = List(1000) { thread { Thread.sleep(1000) } } jobs.forEach { it.join() } }
  13. 13. Lambda + Inline measureTimeMillis { val start = System.currentTimeMillis() val jobs = List(1000) { thread { Thread.sleep(1000) } } jobs.forEach { it.join() } return System.currentTimeMillis() - start }
  14. 14. Properties class Image(val width: Int, val height: Int) { val pixels: IntArray init { pixels = IntArray(this.width * this.height) } }
  15. 15. Properties val image = Image(200, 200) print(image.height) //200
  16. 16. inline fun AppCompatActivity.replaceFragment( tag: String? = null, animation: Boolean = false, factory: (() -> Fragment) ) = supportFragmentManager.beginTransaction().apply { if (animation) { setCustomAnimations(R.anim.fade_in, R.anim.fade_out) } replace(R.id.container, factory(), tag) addToBackStack(null) }.commit() Extension
  17. 17. inline fun AppCompatActivity.replaceFragment( tag: String? = null, animation: Boolean = false, factory: (() -> Fragment) ) = supportFragmentManager.beginTransaction().apply { if (animation) { setCustomAnimations(R.anim.fade_in, R.anim.fade_out) } replace(R.id.container, factory(), tag) addToBackStack(null) }.commit() Extension
  18. 18. override fun onCreate(savedInstanceState: Bundle?) { ... replaceFragment { MyFragment.newInstance() } } Extension
  19. 19. Log.d( TAG, "lineTo, from display point($x, $y), to internal point ($internalX, $internalY)" ) String Template Log.d( TAG, "lineTo, from display point(" + x + "," + y + "), to internal point ( " + internalX + " + "," + internalY + ")" )
  20. 20. Operator Overloading operator fun PointF.plus(pointB: PointF): PointF = PointF(this.x + pointB.x, this.y + pointB.y)
  21. 21. Operator Overloading operator fun PointF.plus(pointB: PointF): PointF = PointF(this.x + pointB.x, this.y + pointB.y)
  22. 22. Operator Overloading val p1 = controlPoint.add(prevPoint)
  23. 23. Operator Overloading val p1 = controlPoint.add(prevPoint) val p1 = controlPoint + prevPoint
  24. 24. Data class data class Customer(var name: String, var email: String) Neither need to create getter/setter manually, nor use the Lombok
  25. 25. Inner Function fun dfs(graph: Graph) { fun dfs(current: Vertex, visited: Set<Vertex>) { if (!visited.add(current)) return for (v in current.neighbors) dfs(v, visited) } dfs(graph.vertices[0], HashSet()) }
  26. 26. Inner Function fun dfs(graph: Graph) { fun dfs(current: Vertex, visited: Set<Vertex>) { if (!visited.add(current)) return for (v in current.neighbors) dfs(v, visited) } dfs(graph.vertices[0], HashSet()) }
  27. 27. No checked Exception try { FileReader file = new FileReader(file); BufferedReader fileInput = new BufferedReader(file); // ... fileInput.readLine(); // ... fileInput.close(); } catch (IOException e) { // handle error }
  28. 28. No checked Exception try { Data data = readData(filename); } catch (IOException e) { // handle error }
  29. 29. No checked Exception Data readData(String filename) throws IOException { FileReader file = new FileReader(filename); BufferedReader fileInput = new BufferedReader(file); // ... fileInput.readLine(); // ... fileInput.close(); }
  30. 30. No checked Exception Data readData(String filename) throws IOException { FileReader file = new FileReader("test.txt"); BufferedReader fileInput = new BufferedReader(file); // ... fileInput.readLine(); // ... fileInput.close(); }
  31. 31. No checked Exception List<Result> results = fileNames.stream() .map(filename -> readData(filename)) .collect(Collectors.toList());
  32. 32. No checked Exception List<Result> results = fileNames.stream() .map(filename -> readData(filename)) .collect(Collectors.toList());
  33. 33. No checked Exception val results = filenames.map { filename -> readData(filename) }
  34. 34. Which projects are using Kotlin in LINE • LINE android: 77% Java, 23% Kotlin • Line Creator Studio (LINE拼貼): 100% Kotlin • Clova SDK • and others...
  35. 35. Summary • If you don’t have any programming experience, learn Java first. • If you are experienced Java developer, you should try Kotlin now. • No doubt Kotlin will become more and more popular.
  36. 36. Q&A

×