CONTRACTS
fun foo(s: String?) {
if (s != null) s.length // Compiler automatically casts 's' to 'String'
}
Slide 7
Slide 7 text
CONTRACTS
fun String?.isNotNull(): Boolean = this != null
fun foo(s: String?) {
if (s.isNotNull()) s.length // No smartcast :(
}
Slide 8
Slide 8 text
CONTRACTS
fun require(condition: Boolean) {
// This is a syntax form, which tells compiler:
// "if this function returns successfully, then passed 'condition' is true"
contract { returns() implies condition }
if (!condition) throw IllegalArgumentException(...)
}
fun foo(s: String?) {
require(s is String)
// s is smartcasted to 'String' here, because otherwise
// 'require' would have throw an exception
}
Slide 9
Slide 9 text
CAPTURANDO VALOR DO
when EM UMA VARIÁVEL
fun Request.getBody() =
when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}
Slide 10
Slide 10 text
@JvmStatic e @JvmField
EM INTERFACES
interface Foo {
companion object {
@JvmField
val answer: Int = 42
@JvmStatic
fun sayHello() {
println("Hello, world!")
}
}
}
Slide 11
Slide 11 text
@JvmStatic e @JvmField
EM INTERFACES
interface Foo {
public static int answer = 42;
public static void sayHello() {
// ...
}
}
Slide 12
Slide 12 text
DECLARAÇÕES ANINHADAS
EM CLASSES DE ANOTAÇÃO
annotation class Foo {
enum class Direction { UP, DOWN, LEFT, RIGHT }
annotation class Bar
companion object {
fun foo(): Int = 42
val bar: Int = 42
}
}
Slide 13
Slide 13 text
MAIN SEM PARÂMETROS
fun main() {
println("Hello, world!")
}
Slide 14
Slide 14 text
ATÉ 255 PARÂMETROS EM
LAMBDAS
fun trueEnterpriseComesToKotlin(block: (Any, Any, ... /* 42 more */, Any) -> Any) {
block(Any(), Any(), ..., Any())
}
Slide 15
Slide 15 text
Experimental
Slide 16
Slide 16 text
INLINE CLASSES
Inline classes can be viewed as a restricted version of the
usual classes, in particular, inline classes must have exactly
one property
The Kotlin compiler will use this restriction to aggressively
optimize runtime representation of inline classes and
substitute their instances with the value of the underlying
property where possible removing constructor calls, GC
pressure, and enabling other optimizations
Slide 17
Slide 17 text
INLINE CLASSES
inline class Name(val s: String)
fun main() {
// In the next line no constructor call happens, and
// at the runtime 'name' contains just string "Kotlin"
val name = Name("Kotlin")
println(name.s)
}
Slide 18
Slide 18 text
UNSIGNED INTEGERS
// You can define unsigned types using literal suffixes
val uint = 42u
val ulong = 42uL
val ubyte: UByte = 255u
// You can convert signed types to unsigned and vice versa via stdlib extensions:
val int = uint.toInt()
val byte = ubyte.toByte()
val ulong2 = byte.toULong()
// Unsigned types support similar operators:
val x = 20u + 22u
val y = 1u shl 8
val z = "128".toUByte()
val range = 1u..5u
Slide 19
Slide 19 text
@JvmDefault
interface Foo {
// Will be generated as 'default' method
@JvmDefault
fun foo(): Int = 42
}
Slide 20
Slide 20 text
Tooling
Slide 21
Slide 21 text
kotlinx.serialization
kotlinx.serialization is a library which provides
multiplatform support for (de)serializing objects in Kotlin.
Previously, it was a separate project, but since Kotlin 1.3, it
ships with the Kotlin compiler distribution on par with the
other compiler plugins. The main difference is that you don't
need to manually watch out for the Serialization IDE Plugin
being compatible with the Kotlin IDE Plugin version you're
using: now the Kotlin IDE Plugin already includes serialization!
Slide 22
Slide 22 text
Como começar a experimentar?
Slide 23
Slide 23 text
Links
WHAT'S COMING IN KOTLIN 1.3
https://kotlinlang.org/docs/reference/whatsnew13.html
A MAJOR RELEASE JUST AROUND THE CORNER - KOTLIN 1.3 M2
https://blog.jetbrains.com/kotlin/2018/08/kotlin-1-3-m2/
SEE WHAT'S COMING IN KOTLIN 1.3 M1
https://blog.jetbrains.com/kotlin/2018/07/see-whats-coming-in-kotlin-1-3-m1/
JETBRAINS TV - KOTLIN CONF 2018
https://www.youtube.com/user/JetBrainsTV
23
Slide 24
Slide 24 text
OBRIGADO
Para perguntas e sugestões:
Rafael Toledo
[email protected]