What's new in Kotlin?

  • Kotlin evolution
“Experimental” features
  • Inline Classes
  • Contracts
  • Channels
  • Flows
  • Multiplatform Projects
展开查看详情

1. Beijing What’s new in Kotlin? Svetlana Isakova @sveta_isakova

2. Kotlin developers 3000 2250 2.2M 1500 750 700K 156K 0 2016 2017 2018

3. Timeline 2010 Project started … … 2016 Kotlin 1.0 2017 Official on Android 2018 Kotlin 1.3

4. Kotlin evolution Kotlin 1.0 Kotlin gets stable! Kotlin 1.1 Coroutines (experimental) Kotlin 1.2 Multi-platform projects (experimental) Kotlin/Native (experimental) Kotlin 1.3 Coroutines get stable! Multi-platform projects can target Kotlin/Native (experimental)

5. Agenda • Kotlin evolution • “Experimental” features

6.Agenda: experimental features • Inline Classes • Contracts • Channels • Flows • Multiplatform Projects

7. Principles of Pragmatic Evolution Language design is cast in stone, but this stone is reasonably soft, and with some effort we can reshape it later. Kotlin Design Team * The real sculpture made by José Manuel Castro López

8.Principles of Pragmatic Evolution • keeping the language modern • comfortable updates • feedback loop

9.KEEPing the language modern https://github.com/Kotlin/KEEP KEEP = Kotlin Evolution and Enhancement Process contains language proposals and the corresponding discussions

10. Comfortable updates • Deprecation warnings in advance • Automatic migration

11.Feedback loop with the community • Kotlin the community • Kotlin team listens to the community • The community members influence the Kotlin evolution

12.Contributors from all over the world …

13.Experimental features

14. Experimental features The goal: to let new features be tried by early adopters as soon as possible

15.Experimental Language Features • can be changed in the future • you need to explicitly opt in at the call site to use experimental features compileTestKotlin { kotlinOptions { freeCompilerArgs += "-Xinline-classes" } }

16. Experimental API for Libraries • can be publicly released as a part of the library • may break at any moment

17. Experimental API You can mark your new class or function as experimental @Experimental annotation class ShinyNewAPI @ShinyNewAPI class Foo { ... }

18.Using experimental API @UseExperimental(ShinyNewAPI::class) fun doSomethingImportant() { val foo = Foo() ... }

19. Experimental: Summary • feedback loop for new features and API

20.Inline classes

21. Inline classes • can wrap values without additional overhead • currently an experimental feature

22. Duration API • KEEP: Duration and Time Measurement API • status: experimental in Kotlin 1.3.50 • uses inline classes

23. Refining API: trying primitive args fun greetAfterTimeout(millis: Long) greetAfterTimeout(2) // meaning 2 seconds?

24. Refining API: trying primitive args fun greetAfterTimeout(millis: Long) greetAfterTimeout(2) // meaning 2 seconds? Too easy to make a mistake

25. Refining API: trying overloads fun greetAfterTimeoutMillis(millis: Long) fun greetAfterTimeoutSeconds(seconds: Long) greetAfterTimeoutSeconds(2)

26. Refining API: trying overloads fun greetAfterTimeoutMillis(millis: Long) fun greetAfterTimeoutSeconds(seconds: Long) greetAfterTimeoutSeconds(2) Too verbose

27. Refining API: trying class class Duration(val millis: Long) fun greetAfterTimeout(duration: Duration) greetAfterTimeout(Duration(millis = 2000))

28. Refining API: trying class class Duration(val millis: Long) fun greetAfterTimeout(duration: Duration) greetAfterTimeout(Duration(millis = 2000)) Extra object is allocated

29. Inline classes to the rescue inline class Duration(val value: Long) fun greetAfterTimeout(duration: Duration) Under the hood: fun greetAfterTimeout_(duration: Long)