Google IO/19-Kotlin 1.3

转载请注明出处

2年前google就确定在kotlin作为android系统的官方语言,Kotlin first。

在刚过去的google IO 2019会议上,又提出了新的kotlin特性,youtube连接如下:
https://www.youtube.com/watch?v=8rYLFD1ZfnM&list=PLWz5rJ2EKKc9FfSQIRXEWyWpHD6TtwxMM&index=6

主要内容如下:

将 when 主语捕获到变量中

原来的kotlin when使用如下:
Google IO/19-Kotlin 1.3
现在可直接使用:
Google IO/19-Kotlin 1.3
注意必须是用val,如果使用var将会发生如下情况:

Google IO/19-Kotlin 1.3

Experimental 注解

Google IO/19-Kotlin 1.3
使用该@Experimental会定义一个注解类,标志该类是实验性的,因此会报错,但是可以更改如下
Google IO/19-Kotlin 1.3
这时就不会发生报错,使用场景可在于多人协作,对于一些不成熟,不稳定的API上的处理。

智能转换

Google IO/19-Kotlin 1.3
然而,一旦将这些检测提取到单独的函数中,所有智能转换都立即消失了:
Google IO/19-Kotlin 1.3
为了改善在此类场景中的行为,Kotlin 1.3 引入了称为 契约 的实验性机制。
Google IO/19-Kotlin 1.3
关键词contract,视频中说法如下:create contracts and say that your function matches your the argument to a function match certain conditions and that basically is guaranteeing you a certain level of code correctness in your actual code

总体就是用contract来保证在实际的代码运行中,参数是匹配你的conditions的。

Json

Google IO/19-Kotlin 1.3

无参的main

无符号整型类型

Google IO/19-Kotlin 1.3

ifEmpty()和ifBlank()

Google IO/19-Kotlin 1.3
以上是使用场景比较多的一些特性,更多请参看Google IO/19 相关视频以及官方文档。