这正是map为了:
def convert(x: Option[Int]) = x map (_.toLong)
Run Code Online (Sandbox Code Playgroud)
其工作方式如下:
scala> convert(Some(1))
res0: Option[Long] = Some(1)
scala> convert(None)
res1: Option[Long] = None
Run Code Online (Sandbox Code Playgroud)
scala.Predef提供从Int到的隐式转换RichInt,这是toLong方法的来源.
| 归档时间: |
|
| 查看次数: |
419 次 |
| 最近记录: |