官术网_书友最值得收藏!

Safe calls

Safe calls let you access methods and properties of nullable values if the value isn't null (under the hood, at the bytecode level, a safe call is transformed into if(x != null)):

nullableCupcake?.eat()

But, what if you use it in an expression?

val result: String? = nullableCupcake?.eat()

It will return null if our value is null, so result must have a String? type.

That opens up the chance to use safe calls on a chain, as follows:

val length: Int? = nullableCupcake?.eat()?.length
主站蜘蛛池模板: 本溪| 佳木斯市| 额济纳旗| 壤塘县| 喀喇沁旗| 凤阳县| 五家渠市| 昆明市| 拉萨市| 察隅县| 拉萨市| 刚察县| 临城县| 保定市| 兰考县| 永丰县| 武陟县| 镇平县| 尤溪县| 顺平县| 蒲江县| 峨边| 武义县| 安多县| 东山县| 武清区| 舞阳县| 丹凤县| 望江县| 泾阳县| 砚山县| 临安市| 元阳县| 金溪县| 桑植县| 庆城县| 财经| 平乐县| 曲阳县| 龙海市| 正安县|