gpt4 book ai didi

Scala Option(null) 预期为 None 但我得到了 Some(0)

转载 作者:行者123 更新时间:2023-12-03 14:48:12 24 4
gpt4 key购买 nike

val i: java.lang.Integer = null
val o: Option[Int] = Option(i) // This yields Some(0)

什么是安全的转换方式 null: java.lang.Integer到斯卡拉 Option[Int] ?

最佳答案

你在混Intjava.lang.Integer所以

val i: java.lang.Integer = null
val o: Option[Int] = Option(i)

隐式转换为
val o: Option[Int] = Option(Integer2int(i))

变成
val o: Option[Int] = Option(null.asInstanceOf[Int])

因此
val o: Option[Int] = Some(0)

如果您想与 java.lang.Integer 合作, 然后写
val o: Option[java.lang.Integer] = Option(i)
// o: Option[Integer] = None

关于Scala Option(null) 预期为 None 但我得到了 Some(0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60600502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com