【发布时间】:2014-01-27 16:04:05
【问题描述】:
此 Stackoverflow post 讨论了如果不将 L 附加到数字时可能出现的数字溢出问题:
这是来自 REPL 的示例:
scala> 100000 * 100000 // no type specified, so numbers are `int`'s
res0: Int = 1410065408
避免此问题的一种方法是使用L。
scala> 100000L * 100000L
res1: Long = 10000000000
或者指定数字的类型:
scala> val x: Long = 100000
x: Long = 100000
scala> x * x
res2: Long = 10000000000
正确指定数字类型的最佳做法是什么?
【问题讨论】:
-
这真是一个意见问题。对此没有普遍接受的最佳实践。我只使用
L后缀。 -
这不是意见问题。添加
L指定正确的类型,然后使用 ascription 转换它。 -
鉴于 Daniel 的评论,为什么我的票被否决了?
标签: scala long-integer