Contenu connexe Similaire à To kotlin or not to kotlin. That's the question (20) To kotlin or not to kotlin. That's the question5. • Readability (functional)
• Reuse (extensible, DSL-friendly)
• Interoperability with JVM
• Safety tooling (except the doc generation tool...)
8. The improvement(?) from Java:
• Nullability
• Lambda + inline
• Properties
• Extension
• String template
• Operator overloading
• Data class
• Inner function
• No checked exception
9. val nullable: String? = "This is the wrong parent"
val nonnuable: String = null //Compiler error
val nullable: String? = null //OK
Nullability
10. Lambda + Inline
public inline fun measureTimeMillis(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
11. Lambda + Inline
public inline fun measureTimeMillis(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
13. Lambda + Inline
measureTimeMillis {
val start = System.currentTimeMillis()
val jobs = List(1000) {
thread {
Thread.sleep(1000)
}
}
jobs.forEach { it.join() }
return System.currentTimeMillis() - start
}
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. 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
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 + ")"
)
24. Data class
data class Customer(var name: String, var email: String)
Neither need to create getter/setter manually, nor use the Lombok
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. 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. No checked Exception
try {
FileReader file = new FileReader(file);
BufferedReader fileInput = new BufferedReader(file);
// ...
fileInput.readLine();
// ...
fileInput.close();
} catch (IOException e) {
// handle error
}
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. No checked Exception
Data readData(String filename) throws IOException {
FileReader file = new FileReader("test.txt");
BufferedReader fileInput = new BufferedReader(file);
// ...
fileInput.readLine();
// ...
fileInput.close();
}
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. 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.
Notes de l'éditeur Tools: IDE, plugin other languages support or Flowable or Flowable or Flowable or Flowable