As a Java developper, you may be looking for new ways to improve your productivity. If one of the answer was to adopt another language for the JVM? Like Kotlin? Let's see what is Kotlin, why is it a good choice and how you can use it in Liferay 7/DXP.
4. What is Kotlin?
● Programming language for JVM and JS
● Statically typed
● Open source
● Created by JetBrains (→ IntelliJ IDEA)
5. What is Kotlin?
2010 2011 2016 2017
Project Kotlin JVM Language
Summit
Release 1.0 Android support
{ WORKING }
Release 1.1
Release 1.2
6. What is Kotlin?
JetBrains concerns:
● To be more productive
● Other languages have features or speed, but not both
● To drive the sales of IntelliJ IDEA
10. Why Kotlin?
data class Greeting(val name: String)
fun main(args: Array<String>) {
val greeting = Greeting("world")
println("Hello, ${greeting.name}!")
}
11. Why Kotlin?
data class Greeting(var name: String = "") {
fun sayHello() = println("Hello, $name!")
}
fun main(args: Array<String>) {
val greeting = Greeting()
greeting.name = "world"
greeting.sayHello()
}
13. Why Kotlin?
class Name(val value: String) {
operator fun plus(name: Name) {
return Name("${this.value} ${name.value}")
}
}
14. Why Kotlin?
val firstName = Name("John")
val lastName = Name("Doe")
val fullName = firstName.plus(lastName)
println(fullName.value) // prints "John Doe"
15. Why Kotlin?
@Deprecated(
level = DeprecationLevel.WARNING, // ERROR, HIDDEN
message = "We use anotherMethod() now",
replaceWith = ReplaceWith("anotherMethod()")
)
fun someMethod()
fun anotherMethod()
20. How to use Kotlin in Liferay?
buildscript {
dependencies {
classpath group: "org.jetbrains.kotlin",
name: "kotlin-gradle-plugin",
version: "1.1.2"
}
}
apply plugin: 'kotlin'
21. How to use Kotlin in Liferay?
dependencies {
compileOnly group: "org.jetbrains.kotlin",
name: "kotlin-osgi-bundle",
version: "1.1.2"
}
22. How to use Kotlin in Liferay?
Import-Package:
!kotlin.*,
*
Conditional-Package:
kotlin.*
→ Add an import statement for a
package that is not referred to by your
code but is still needed
→ Recursively add packages from the
class path when referred and when they
match one of the package
specifications
23. How to use Kotlin in Liferay?
@Component(
immediate = true,
property = arrayOf(
"com.liferay.portlet.instanceable=true"
// and much more...
),
service = arrayOf(Portlet::class)
)
class KotlinGreeterPortlet : MVCPortlet() { // ... }
24. How to use Kotlin in Liferay?
class KotlinGreeterPortlet : MVCPortlet() {
@Throws(IOException::class, PortletException::class)
override fun doView(
renderRequest: RenderRequest,
renderResponse: RenderResponse) {
// do awesome stuff with Kotlin...
}
}
25. How to use Kotlin in Liferay?
https://github.com/liferay/liferay-blade-samples
26. Conclusion
● Harder
● Better
● Faster
● Stronger
● An introduction to Kotlin by
example, Dmitry Kandalov
● 10 Kotlin Tricks in 10 ish minutes,
Jake Wharton
● Kotlin in Action, Dmitry Jemerov,
Svetlana Isakova
Unveiled at the JVM Language Summit in July 2011
Dmitry Jemerov said they’ve been working on it for almost a year : https://blog.jetbrains.com/kotlin/2011/07/hello-world-2/
1.1 & 1.2 in 2017
https://blog.jetbrains.com/kotlin/2011/08/why-jetbrains-needs-kotlin/
Google IO 2017 → Android officially supports Kotlin
Android Studio is developped by JetBrains
fun -> concise, name: Type, no semilicon, package level
type inferrence : returns Unit here
inline class declaration, data (equals, hashcode, toString, copy = clone), no new keyword, string templates
inline function, default arguments value, string template without curly braces
null safety → can be null only explicitly, null safety with accessor
operator overloading
operator overloading
deprecated levels → WARNING = same as java, ERROR = compilation error, HIDDEN = like method doesn’t exist
replace with → easy replacement with IntelliJ IDEA