Questions tagged [scala-generics]
60 questions
16
votes
2 answers
Why wrapping a generic method call with Option defers ClassCastException?
Lets say I have an array like this*:
val foo: Any = 1 : Int
Option(foo.asInstanceOf[String])
which fails for obvious reason:
// java.lang.ClassCastException: java.lang.Integer cannot be cast to
// java.lang.String
// ... 48 elided
Next lets…

zero323
- 322,348
- 103
- 959
- 935
9
votes
1 answer
Avoiding redundant generic parameters in Scala
So this is a fairly direct port of this Java question to scala
We have a bunch of traits that take generic parameters as follows:
trait Ident { }
trait Container[I <: Ident] {
def foo(id: I): String
}
trait Entity[C <: Container[I], I <:…

Tom Shackell
- 93
- 4
7
votes
2 answers
scala generic function return type
I tried writing a function with a generic return type but it doesn't work unless I cast the return type. Please see the function getSomething() below I expected it to work without the casting. What might I be doing wrong here?
trait Sup
class Sub…

inglorious_engineer
- 97
- 1
- 5
5
votes
1 answer
Self-type does not conform to base class
Using the following code:
trait Hello[B <: Baz[_, _]]
trait World[F <: Foo] { self: Hello[Baz[F, _]] =>
def foo: F
}
trait Baz[F <: Foo, B <: Bar]
trait Foo
trait Bar
case class BasicHello() extends Hello[BasicBaz] with World[BasicFoo]
case…

Martijn
- 2,268
- 3
- 25
- 51
4
votes
2 answers
Strange Scala 'Type mismatch' error for tuples
I have a function map which takes a Parser and a function defined as follows:
def map[T1, T2](parser: Parser[T1], func: T1 => T2): Parser[T2]
I've created a Parser object of type [(Char, Char)] and a function (Char, Char) => String.
val…

Durga Swaroop
- 563
- 1
- 6
- 25
4
votes
2 answers
Declare a generic class in scala without square brackets
When reading this article I came to the following syntax:
implicit val slaveCanRead: Slave HasPrivilege Read = null
The author says:
Also, please not that Slave HasPrivilege Read is just another notation for HasPrivilege[Slave, Read]
Keeping the…

Tadej Mali
- 1,143
- 8
- 18
4
votes
2 answers
How to "extract" type parameter to instantiate another class
The following Scala code works:
object ReducerTestMain extends App {
type MapOutput = KeyVal[String, Int]
def mapFun(s:String): MapOutput = KeyVal(s, 1)
val red = new ReducerComponent[String, Int]((a: Int, b: Int) => a + b)
val data =…

dividebyzero
- 2,190
- 1
- 21
- 33
3
votes
1 answer
"ambiguous reference to overloaded definition" when using Java class in Scala
I am using a Java class in my Scala which generates ambiguous reference to overloaded definition. Here is the code to explain this problem.
IComponent.java
package javascalainterop;
import java.util.Map;
public interface IComponent {
…

doodlegum
- 33
- 4
3
votes
2 answers
Scala: how to force type to be provided
Lets say we have the following trait and class definition
trait Model extends Product
class X[T <: Model] {}
Give the above I can create instance of X as follows.
val x = new X
The compiler does not complain. The inferred type in this case is…

ehsan
- 103
- 2
- 7
3
votes
1 answer
Why scala serializability differs in case classes with same constructor parameter types?
Why am I able to serialize this:
// Serialize: OK
case class ClassWithType2[T:TypeTag](x:T) {
val tpe:java.lang.reflect.Type = Util.toJavaClass[T]
}
... but not this
class TypeAware[T:TypeTag]() {
val tpe:java.lang.reflect.Type =…

user48956
- 14,850
- 19
- 93
- 154
2
votes
2 answers
Scala compiler is complaining about type mismatch for generic parameter on method level
Why Scala compiler cannot compile next code :
trait Profile {}
class SomeProfile extends Profile
trait Foo {
def get[T <: Profile]: Option[T]
}
object Example {
val foo: Foo = new Foo {
// This works (but might give runtime exception), but…

Artavazd Balayan
- 2,353
- 1
- 16
- 25
2
votes
1 answer
How to return the original collection type from generic method
I have a generic method that should return a collection of the same type as input:
def removeN[A, C <: Seq[A]](s: C, n: Int): C = {
s.take(n) ++ s.drop(n + 1) // Sample operation
}
But this code does not compile:
Error:(34, 15) type mismatch; …

Alexey Sirenko
- 452
- 3
- 12
2
votes
1 answer
Scala generic type usage in Apache Spark Dataset creation
The following code creates a empty Dataset in Spark..
scala> val strings = spark.emptyDataset[String]
strings: org.apache.spark.sql.Dataset[String] = [value: string]
The signature of emptyDataset is..
@Experimental
…

Madhu
- 21
- 3
2
votes
1 answer
Scala Define a generic class that only accepts case classes
When defining a generic class in Scala how can I ensure that the type parameter 'A' must be a case class?
class TypedCollection[A](name: String){}
Context:
I'm trying to define a generic 'TypedCollection' wrapper class to interact a reactivemongo…

Oliver Rice
- 768
- 8
- 20
2
votes
1 answer
Pattern matching on generic abstract type in scala
I have a self-recursive type:
trait Problem[P <: Problem[P]] {
type Solution
}
Now, I want to pattern match on P#Solution. Let's assume we're within the case class Foo[P <: Problem[P]]():
case ExampleCaseClass(s: P#Solution) if conditionApplies()…

slnowak
- 1,839
- 3
- 23
- 37