Scala中的类型安全原语

Mik*_*ike 8 scala

我想在我的Scala代码中使用基本类型安全的"子类",而不会出现拳击的性能损失(对于非常低延迟的应用程序).例如,像这样:

class Timestamp extends Long
class ProductId extends Long

def process(timestamp: Timestamp, productId: ProductId) {
  ...
}

val timestamp = 1: Timestamp // should not box
val productId = 1: ProductId // should not box

process(timestamp, productId) // should compile
process(productId, timestamp) // should NOT compile
Run Code Online (Sandbox Code Playgroud)

去年Scala用户邮件列表上有一个帖子似乎得出结论,没有拳击是不可能的,但我想知道现在Scala 2.8是否可行.

Dan*_*ral 1

这种事情可以通过插件来保证。毕竟,Scala 不受支持且无法工作的单位插件在阻止将距离添加到持续时间中时做了类似的事情。