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

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
主站蜘蛛池模板: 巫山县| 襄汾县| 体育| 牟定县| 内黄县| 龙江县| 锦屏县| 湘乡市| 黎城县| 天等县| 太湖县| 泸定县| 彰武县| 即墨市| 凤城市| 剑阁县| 长春市| 中方县| 泾源县| 财经| 茌平县| 湘潭县| 新巴尔虎左旗| 剑阁县| 阜康市| 崇文区| 滁州市| 穆棱市| 徐州市| 安图县| 苗栗县| 麦盖提县| 望奎县| 宽城| 虎林市| 股票| 永川市| 凤城市| 商都县| 宣城市| 门头沟区|