How can I use primitives in Scala?

后端 未结 2 1674
傲寒
傲寒 2020-12-17 15:07

Can I use primitives in Scala?

The use case is for storing billions of ints, so the difference between 4 bytes (for an int) and 16 bytes (for an Integer) is import

相关标签:
2条回答
  • 2020-12-17 15:39

    If you want Scala to store unboxed primitives, you could use Array[Int] but refrain from using any cool Scala collection method on it (because it will force boxing).

    If you look for immutable collections of primitives types, you can have a look at Debox, which provides specialised Buffers, Sets and Maps. The project is still evolving but it is very promising.

    0 讨论(0)
  • 2020-12-17 15:45

    You can use the @specialised annotation to let the compiler create specialised instances of a class for you. See this article.

    class Container[@specialized(Int) T](value: T) {
      def apply(): T = value
    }
    
    0 讨论(0)
提交回复
热议问题