Questions tagged [specialized-annotation]

14 questions
43
votes
3 answers

Why are so few things @specialized in Scala's standard library?

I've searched for the use of @specialized in the source code of the standard library of Scala 2.8.1. It looks like only a handful of traits and classes use this annotation: Function0, Function1, Function2, Tuple1, Tuple2, Product1, Product2,…
Jesper
  • 202,709
  • 46
  • 318
  • 350
30
votes
4 answers

Specialization of generic functions in Scala (or Java)

Is it possible to specialize generic functions (or class) in Scala? For example, I want to write a generic function that writes data into a ByteBuffer: def writeData[T](buffer: ByteBuffer, data: T) = buffer.put(data) But as the put method takes…
K J
  • 4,505
  • 6
  • 27
  • 45
12
votes
1 answer

Implementing a fixed size, immutable, and specialized vector

For performance and safety I would like to implement a fixed-size vector which is both immutable and specialized (I need fast arithmetics). My first idea was to use the @specialized annotation (because I need both integers and reals). Here is a…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
4
votes
0 answers

Is it possible to obtain the effect of a specialized value class in scala?

I know how @specialized classes and value classes work and I know that I can't have a specialized value class as in: class MilliVolt[@specialized T](val content :T) extends AnyVal //illegal I can't find a way to obtain a similiar effect: with a…
Turin
  • 2,208
  • 15
  • 23
3
votes
1 answer

Pattern matching with specialized in Scala

I have a class that must work with Double and Float. I use generic with @specialized annotation for (Double, Float) due to performance requirements. There are two third-party functions, that I need to call. ffunc(x: Float) accepts Float, dfunc(y:…
2
votes
1 answer

Scala @specialized annotation infinite recursion?

Version: scala 2.11.8 I defined a class with specialized type and override method in inheritance: class Father[@specialized(Int) A]{ def get(from: A): A = from } class Son extends Father[Int]{ override def get(from: Int): Int = { …
2
votes
1 answer

@specialized + inner classes not allowed in scala?

When I compile the following: import org.junit.Test class TypesTest{ @Test def specializedTest(): Unit = { trait TTBase[@specialized T] { def evaluate:T } case class TTX[@specialized T](value:T) extends TTBase[T] { …
user48956
  • 14,850
  • 19
  • 93
  • 154
2
votes
0 answers

Scala @specialized annotation on function doesn't generate specialized type in pattern matching

I have the following function in scala, I intend to use it to convert any traversable into array and wrap any non-traversable with array: def asArray[@specialized(scala.Int, scala.Long, scala.Float, scala.Double/*, scala.AnyRef*/) T <: Any :…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
2 answers

scala specialized general numeric operations

I want a class that encapsulates both integral and floating point numbers. Basically I want to be able to do something like this Const(5) + Const(6) = Const(11) Const(5) + Const(6.0) = Const(11.0) The code with compiler errors. case class…
smartnut007
  • 6,324
  • 6
  • 45
  • 52
2
votes
2 answers

How to obtain the raw datatype of a parameter of a field that is specialized in Scala via reflection?

I have a class with a field that is specialized and is using a raw datatype. For instance a Tuple2[Int, String]: scala> class TupleReflection(val tuple: Tuple2[Int, String]) defined class TupleReflection scala> val refl = new TupleReflection((5,…
Tomas Lazaro
  • 315
  • 1
  • 8
1
vote
0 answers

Methods in trait are not getting specialized

Let the following setup: sealed trait Test[@specialized T] { implicit protected def tag: ClassTag[T] def xs: Array[T] def count(condition: T => Boolean): Int = { @tailrec def innerLoop(index: Int, acc: Int): Int = if (index ==…
sanyi14ka
  • 809
  • 9
  • 14
1
vote
0 answers

Benefit for @specialized in template base where template parameter known at call?

Suppose I have: trait Normalizer[T]{ def apply(x:T):T } case class DoubleNormalizer() extends Normalizer[Double] { def apply(x:Double):Double { ... impl ... } } Does boxing/unboxing happen in either of the following cases, and would…
user48956
  • 14,850
  • 19
  • 93
  • 154
0
votes
1 answer

Proper work to specialize generic classes in Python

I am having a hard time using python typing annotations when dealing with generics and compound types Consider the following class: import typing as ty T = ty.TypeVar("T") CT = tuple[bool, T, str] class MyClass(ty.Generic[T]): internal1:…
Hernan
  • 5,811
  • 10
  • 51
  • 86
0
votes
1 answer

Why can't I specialize a generic function in scala?

I get an error saying type N is unused or used in non-specializable positions., for the method with the following signature: protected def offsetFrom0[@specialized(Int,Long) N](offsetFrom1 : Codec[N])(implicit N : Integral[N]) : Codec[N] Can…
Luciano
  • 2,388
  • 1
  • 22
  • 33