A Look Into the Future Roman Elizarov October 12, 2020 @relizarov - - PowerPoint PPT Presentation

a look into the future roman elizarov
SMART_READER_LITE
LIVE PREVIEW

A Look Into the Future Roman Elizarov October 12, 2020 @relizarov - - PowerPoint PPT Presentation

Kotlin 1.4 Online Event A Look Into the Future Roman Elizarov October 12, 2020 @relizarov A bit of history Kotlin 1.0 2016 Posted on February 15, 2016 by Andrey Breslav Kotlin 1.1 Released with JavaScript Support, Coroutines and more


slide-1
SLIDE 1

Kotlin 1.4 Online Event October 12, 2020 @relizarov

A Look Into the Future Roman Elizarov

slide-2
SLIDE 2

A bit of history

slide-3
SLIDE 3

Posted on February 15, 2016 by Andrey Breslav 2016

Kotlin 1.0

slide-4
SLIDE 4

2017

Kotlin 1.1 Released with JavaScript Support, Coroutines and more

Posted on March 1, 2017 by Roman Belov

slide-5
SLIDE 5

Posted on November 28, 2017 by Dmitry Jemerov

Kotlin 1.2 Released: Sharing Code between Platforms

2017

slide-6
SLIDE 6

2018 Posted on October 29, 2018 by Roman Belov

Kotlin 1.3 Released with Coroutines, Kotlin/Native Beta, and more

slide-7
SLIDE 7

2020 Posted on August 17, 2020 by Svetlana Isakova

Kotlin 1.4 Released with a Focus

  • n Quality and Performance
slide-8
SLIDE 8

Near future

Plans

slide-9
SLIDE 9

Sharing code

slide-10
SLIDE 10

JVM server/interoperability

slide-11
SLIDE 11

Java interoperability: upcoming

  • All new Java APIs: seamless interop
  • JEP 359: Records (Preview)
  • JEP 384: Records (Second Preview)
  • JEP 360: Sealed Classes (Preview)
slide-12
SLIDE 12

Driving forces

slide-13
SLIDE 13

Driving forces: what to focus on?

Community

  • Comm. with

big users Slack & Forums YouTrack KEEP

slide-14
SLIDE 14

YouTrack vs KEEP

  • KEEP

design documents

  • Worked out and prototyped
  • KEEP issues

corrections

https://kotl.in/issue

  • Problems, ideals, proposals

YouTrack #{language design}

slide-15
SLIDE 15

YouTrack: language design

1.1 1.2 1.3 1.4

slide-16
SLIDE 16

Distant future

Speculative, we are looking for feedback

slide-17
SLIDE 17

The most voted request now

slide-18
SLIDE 18

KT-11968: Statically accessible extensions

All Kotlin extensions are resolved statically

slide-19
SLIDE 19

KT-11968: Statically accessible extensions

What are you trying to achieve?

val Intent.Companion.SCHEME_SMS: String get() = "sms"

3rd-party type Code with receiver Companion type

slide-20
SLIDE 20

KT-11968: Statically accessible extensions

val Intent.Companion.SCHEME_SMS: String get() = "sms" Intent.SCHEME_SMS

What are you trying to achieve?

slide-21
SLIDE 21

Why object is used here?

Similar/related problem

  • bject Delegates {

fun <T : Any> notNull(): ... // other declarations }

What are you trying to achieve?

slide-22
SLIDE 22

Similar/related problem

Delegates.notNull()

  • bject Delegates {

fun <T : Any> notNull(): ... // other declarations }

What are you trying to achieve?

slide-23
SLIDE 23

What is object?

  • bject Delegates {

fun <T : Any> notNull(): ... // other declarations }

  • Instance

val x = Delegates

  • Namespace

Delegates.notNull()

  • Type

x is Delegates

slide-24
SLIDE 24

What is object?

  • bject Delegates {

fun <T : Any> notNull(): ... // other declarations }

  • Instance

val x = Delegates

  • Type

x is Delegates

  • Namespace

Delegates.notNull() Library maintenance burden

slide-25
SLIDE 25

What if you could declare just a namespace?

  • bject Delegates {

fun <T : Any> notNull(): ... // other declarations }

  • Namespace

Delegates.notNull()

slide-26
SLIDE 26

What if you could declare just a namespace?

namespace Delegates { fun <T : Any> notNull(): ... // other declarations }

  • Namespace

Delegates.notNull()

slide-27
SLIDE 27

Enables: companion namespaces

class Example { companion object { private val SOME_CONST = … } }

slide-28
SLIDE 28

Enables: companion namespaces

class Example { namespace { private val SOME_CONST = … } }

slide-29
SLIDE 29

Enables: namespaces extensions

val Intent.Companion.SCHEME_SMS: String get() = "sms"

slide-30
SLIDE 30

Enables: namespaces extensions

val namespace<Intent>.SCHEME_SMS: String get() = "sms"

Code without receiver

Intent.SCHEME_SMS

What we wanted!

slide-31
SLIDE 31

Multiple receivers

slide-32
SLIDE 32
slide-33
SLIDE 33

Member extensions

class View { fun Float.dp() = this * resources.displayMetrics.density }

Float View

slide-34
SLIDE 34

KT-10468: Multiple receivers

fun (View, Float).dp() = this * resources.displayMetrics.density

slide-35
SLIDE 35

KT-10468: Multiple receivers

fun View.Float.dp() = …

slide-36
SLIDE 36

KT-10468: Multiple receivers

fun Float.dp(implicit view: View) = …

slide-37
SLIDE 37

Syntactic analogy

with(view) { 42f.dp() }

slide-38
SLIDE 38

Syntactic analogy

with<View> fun Float.dp() = this * resources.displayMetrics.density

slide-39
SLIDE 39

Take it further

inline fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } }

slide-40
SLIDE 40

Take it further

inline fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } }

slide-41
SLIDE 41

Take it further

inline fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } }

slide-42
SLIDE 42

Take it further

inline fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } } fun doSomething() { withTransaction { // code } } No magic

slide-43
SLIDE 43

Decorators

inline decorator fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } } fun doSomething() { withTransaction { // code } }

slide-44
SLIDE 44

Decorators

inline decorator fun <T> withTransaction(block: () -> T): T { val tx = beginTransaction() return try { block() } finally { tx.commit() } } @withTransaction fun doSomething() { // code } The best

  • f two

worlds

slide-45
SLIDE 45

Decorators with receivers

inline decorator fun <T> Tx.withTransaction(block: () -> T): T { begin() return try { block() } finally { commit() } } @withTransaction fun doSomething() { // code }

Gets additional receiver Tx

slide-46
SLIDE 46

Decorators with receivers

@with<View> fun Float.dp() = this * resources.displayMetrics.density Just a standard decorator!

slide-47
SLIDE 47

Public/private property types

It does not have to be complicated

slide-48
SLIDE 48

Minimal design needed

slide-49
SLIDE 49

Ternary operator

slide-50
SLIDE 50
  • Kotlin has “if” expression

if (foo) a else b

  • Kotlin consistently uses “?” in the context of nullability

foo ?: b

  • Boolean abuse in APIs

Declined Hard for existing code Hard for novices, inconsistent When to use which Do you write nullable

  • r Boolean before ?

The goal of Kotlin is to enable type-safe APIs

slide-51
SLIDE 51

Immutability

Cross-cutting trend

slide-52
SLIDE 52

Mutable data

| Declare

data class State( var lastUpdate: Instant, var tags: List<String> )

| Share

notifyOnChange(state.copy())

| Update

state.lastUpdate = now() state.tags += tag

slide-53
SLIDE 53

Immutable data

| Declare

data class State( val lastUpdate: Instant, val tags: List<String> )

slide-54
SLIDE 54

Immutable data

| Declare

data class State( val lastUpdate: Instant, val tags: List<String> )

| Share

notifyOnChange(state)

| Update

state = state.copy( lastUpdate = now(), tag = state.tags + tag )

slide-55
SLIDE 55

Can we have cake and eat it, too?

No stable identity

Value-based class

val class State( val lastUpdate: Instant, val tags: List<String> )

| Declare

slide-56
SLIDE 56

Can we have cake and eat it, too?

Copying syntax sugar

val class State( val lastUpdate: Instant, val tags: List<String> )

| Declare | Update

state.lastUpdate = now() state.tags += tag

slide-57
SLIDE 57

notifyOnChange(state)

Can we have cake and eat it, too?

val class State( val lastUpdate: Instant, val tags: List<String> )

| Declare | Update

state.lastUpdate = now() state.tags += tag

| Share

slide-58
SLIDE 58

Experimental inline classes

inline class Color(val rgb: Int)

Confusing with Valhalla inline

slide-59
SLIDE 59

Stable future for experimental inline classes

@__TBD__ val class Color(val rgb: Int)

No stable identity

slide-60
SLIDE 60

Stable future for experimental inline classes

@__TBD__ val class Color(val rgb: Int)

No stable identity Optimize away boxes when possible

slide-61
SLIDE 61

Other contributions to Kotlin features

slide-62
SLIDE 62

Compiler

Sources JVM JS LLVM

FE Plugins (declare, resolve) BE Plugins (codegen)

Frontend Common BE

slide-63
SLIDE 63

JetPack Compose

@Composable fun Greeting(name: String) { Surface(color = Color.Yellow) { Text(text = "Hello $name!") } }

slide-64
SLIDE 64

JetPack Compose

@Composable fun Greeting(name: String) { Surface(color = Color.Yellow) { Text(text = "Hello $name!") } }

A language feature

slide-65
SLIDE 65

Differentiable programming @Facebook

@Differentiable fun foo(x: Float, y: Float): Float { val a = x * y val b = a + 5f val c = b * b * b return c }

Automatic differentiation

slide-66
SLIDE 66
slide-67
SLIDE 67

Conclusion

slide-68
SLIDE 68

Recap

  • JVM interop commitment
  • Namespaces and extensions
  • Multiple receivers
  • Public/private property types
  • Ternary operator
  • Immutability and inline classes
  • Other contributions
slide-69
SLIDE 69

What else we are looking at?

  • More concise syntax for algebraic types
  • Data literals (collection literals, tuples, etc)
  • Even more flexible properties
  • Better API evolution/maintenance facilities
  • Constant evaluation/folding
  • And more!
slide-70
SLIDE 70

@relizarov

Thanks! Have a nice Kotlin