Share
Embed
Transcript
24 slides
-
CONTRACTS fun foo(s: String?) { if (s != null) s.length
// Compiler automatically casts 's' to 'String' } -
CONTRACTS fun String?.isNotNull(): Boolean = this != null fun foo(s:
String?) { if (s.isNotNull()) s.length // No smartcast :( } -
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 } -
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) } -
@JvmStatic e @JvmField EM INTERFACES interface Foo { companion object
{ @JvmField val answer: Int = 42 @JvmStatic fun sayHello() { println("Hello, world!") } } } -
@JvmStatic e @JvmField EM INTERFACES interface Foo { public static
int answer = 42; public static void sayHello() { // ... } } -
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 } } -
ATÉ 255 PARÂMETROS EM LAMBDAS fun trueEnterpriseComesToKotlin(block: (Any, Any, ...
/* 42 more */, Any) -> Any) { block(Any(), Any(), ..., Any()) } -
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 -
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) } -
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 -
@JvmDefault interface Foo { // Will be generated as 'default'
method @JvmDefault fun foo(): Int = 42 } -
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! -
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