我知道在编译时内嵌在scala中的value class
。斯卡拉值类,用例
也许这样
case class A(i: Int) extends AnyVal {
def +(that: A) = A(this.i + that.i)
}
A(1) + A(2) // After compile it equals to 1 + 2
,但它似乎不是什么大不了的事给我。
它可能提高性能,但是,
调用this.i + that.i
不似乎远远低于i + i
为什么我们需要在斯卡拉value class
和任何使用情况???
*调用this.i + that.i似乎并不比我慢+ *你是如何确定这一点的?你是否微调了这些代码? –
除了调用更快的事实(无论如何,最可能由JIT编译器内联),值类避免了对象实例化,因此减少了垃圾收集负载。 – Suma