Kotlin: Why Do You Care?
-
Upload
intelliyole -
Category
Software
-
view
2.883 -
download
1
Transcript of Kotlin: Why Do You Care?
Kotlin in the CommunityTeamProductivity Tools
LanguagesKotlin
MPS
TeamCity
YouTrack
Upsource
IDEsIntelliJ IDEA
PhpStorm
PyCharm
RubyMine
WebStorm
AppCode
CLion
.NET & Visual Studiodeveloper productivity tools
ReSharper
ReSharper С++
dotCover
dotTrace
dotMemory
dotPeek
3
• Statically typed programming language targeting the JVM
• Support for functional and OO paradigms
• Pragmatic, safe, concise, great Java interop
• Free and open-source
• In public beta, 1.0 coming Real Soon Now
4
Outline• Why Kotlin was created
• Kotlin feature highlights
• How and why we’re using Kotlin
• Where Kotlin works best
• How you can benefit from Kotlin in your projects
5
Why was Kotlin created?
6
“We’ve built tools to support so many nice languages, and we’re still using Java”
7
JetBrains, 2010
Developer Productivity
8
Develop with Pleasure
9
Thought Leadership
• Programming languages are a popular discussion topic
• IntelliJ IDEA is always going to have best support for Kotlin
10
Good chance of success
• Technical: experience building intelligent tools to support other languages
• Marketing: company reputation and user trust
11
Kotlin Feature Highlights
12
Primary Constructorclass Person(val firstName: String, val lastName: String) {
}
Propertiesclass Person(val firstName: String, val lastName: String) { val fullName: String get() = "$firstName $lastName" }
Propertiesclass Person(val firstName: String, val lastName: String) { val fullName: String get() = "$firstName $lastName" }
Smart Castsinterface Exprclass Constant(val value: Double) : Exprclass Sum(val op1: Expr, val op2: Expr) : Expr
Smart Castsfun eval(e: Expr): Double = when(e) {
}
Smart Castsfun eval(e: Expr): Double = when(e) {
}
Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value
}
Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value
}
class Constant(val value: Double) : Expr
Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value is Sum -> eval(e.op1) + eval(e.op2) else -> throw IllegalArgumentException()}
class Sum(val op1: Expr, val op2: Expr) : Expr
Extension Functions
fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()
Extension Functions
fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()
Extension Functions
fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()
Nullability
Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) {
}
Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { val parentDepth = if (parent != null) parent.depth() else 0 return parentDepth + 1 } }
Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { return (parent?.depth() ?: 0) + 1 } }
Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { return (parent?.depth() ?: 0) + 1 } }
Collections and Lambdas
fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName }
Collections and Lambdas
fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName }
Collections and Lambdas
fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName } .sortBy { it.firstName }
Collections and Lambdas
fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName } .sortBy { it.firstName } .map { "${it.firstName} ${it.lastName}” }
How JetBrains Uses Kotlin
34
Projects Using Kotlin• Kotlin
• JetProfile (CRM, sales and license management)
• IntelliJ IDEA 15
• YouTrack v.next
• 2 new unannounced projects
• Research projects related to bioinformatics
• Plugins, Intranet services etc.
35
JetProfile
36
Проект JetProfile
37
JetProfile Project
• Started in July 2013
• Replaces old Spring-based system
• 100% Kotlin, 100k LOC
• Team of 6 developers
38
IntelliJ IDEA
39
Kotlin in IntelliJ IDEA
• In tests since version 14, in production since 15
• 74k LOC in Kotlin, out of which 18k in tests
• A few developers actively using Kotlin
• Kotlin team provides support with updating to new Kotlin versions
40
Where Kotlin Works Best
The Strengths of Kotlin
• Modeling the data of your application concisely and expressively
• Creating reusable abstractions using functional programming techniques
• Creating expressive domain-specific languages
Kotlin on the Server Side
• Java interop ensures that all existing Java frameworks can be used
• No rewrite required to start using Kotlin in existing codebase
• Existing investment is fully preserved
Server-side Kotlin Frameworks
• Kara: Web framework powering JetProfile https://github.com/shafirov/kara
• Exposed: Database access framework https://github.com/JetBrains/exposed
• Ktor: Experimental next generation Web framework https://github.com/kotlin/ktor
Kara: HTML Buildersfun HtmlBodyTag.renderPersonList( persons: Collection<Person>) { table { for (person in persons) { tr { td { +person.name } td { +person.age } } } } }
45
Kara: HTML Buildersfun HtmlBodyTag.renderPersonList( persons: Collection<Person>) { table { for (person in persons) { tr { td { +person.name } td { +person.age } } } } }
46
Exposed: Table structure
object CountryTable : IdTable() { val name = varchar("name", 250) .uniqueIndex()
val iso = varchar("iso", 3) .uniqueIndex() val currency = varchar("currency", 3) }
47
Exposed: Entities
class Country(id: EntityID) : Entity(id) { var name: String by CountryTable.name val allCustomers by Customer.referrersOn( CustomerTable.country_id) }
48
Exposed: Queries
val germany = Country .find { CountryTable.iso eq "de" } .first()
49
Kotlin for Android• Compatible with Java 6
• Small runtime library
• Great tooling in Android Studio
• Compatible with existing Java libraries and tools
• 100% Java performance
Kotlin Tools for Android
• Kotlin Android Extensions: no more findViewById()
• Anko: DSL for UI, extension functions https://github.com/jetbrains/anko
• KApt: annotation processing
Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }
Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }
Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }
Anko: Android UI Builders
tableLayout { for (location in locations) { tableRow { textView(text = location.name) .lparams(column = 1) } }}
Kotlin in the Community
How to succeed with Kotlin
57
Take initiative
58
Take initiative
• Kotlin is not a silver bullet and not a magical solution for the problems of your project
• Kotlin makes you more productive and your work more enjoyable
• Try it and share the results with your teammates
59
Don’t wait for a new project
• Kotlin can be easily integrated into existing Java codebases
60
Where to start using Kotlin
• Tests - OK not informative
• Plugins and utilities - better
• Core and frameworks - best
61
Don’t fear the learning curve
• At JetBrains, summer interns become productive with Kotlin very quickly and deliver great results
62
Use the Java to Kotlin converter
• Convert only the classes that you’re going to modify
• Cleanup and refactor the code produced by the converter
63
Use the Java to Kotlin converter
64
Summary
• JetBrains entrusts the most business-critical tasks to Kotlin
• Kotlin works great for both server-side and Android development
• Take initiative and start using Kotlin today!
65