scala-generics

Scala - Covariance

妖精的绣舞 提交于 2019-12-13 01:59:12
问题 According to covariance definition: Q[+B] means that Q can take any class, but if A is a subclass of B, then Q[A] is considered to be a subclass of Q[B]. Let's see the following example: trait SomeA trait SomeB extends SomeA trait SomeC extends SomeB case class List1[+B](elements: B*) val a = List1[SomeA](new SomeA{},new SomeB{}) val b = List1[SomeB](new SomeB{},new SomeC{}) Everything is fine, but I don't see why List1[SomeB] is a subclass of List1[SomeA] , or in other words why b is a

scala generic function return type

北城余情 提交于 2019-12-06 01:27:00
问题 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 extends Sup { def getString = "I am Sub" } class Sub2 extends Sup { def getInt = 100 } def getSomething[A <: Sup](str: String) : A = { str match { case "sub" => getSub.asInstanceOf[A] case "sub2" => getSub2.asInstanceOf[A] } } def getSub(): Sub = {

Avoiding redundant generic parameters in Scala

こ雲淡風輕ζ 提交于 2019-12-05 04:09:17
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 <: Ident] { def container: C def foo(id: I) = container.foo(id) } This works but it's a little clumbsy, since we have to provide the type of the Ident and the type of the Container when defining a sub-class of Entity. When in fact just the type of the Container would be enough type information by itself: class MyIdent extends Ident { } class MyContainer extends

Why wrapping a generic method call with Option defers ClassCastException?

若如初见. 提交于 2019-11-30 08:54:39
问题 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 consider a following class: case class DummyRow() { val foo: Any = 1 : Int def getAs[T] = foo.asInstanceOf[T] def getAsOption[T] = Option(foo.asInstanceOf[T]) } As far as I can tell getAs should behave the same way as the previous apply followed by

How to “extract” type parameter to instantiate another class

巧了我就是萌 提交于 2019-11-29 22:09:11
问题 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 = List[String]("a", "b", "c", "b", "c", "b") data foreach {s => red(mapFun(s))} println(red.mem) // OUTPUT: Map(a -> 1, b -> 3, c -> 2) } class ReducerComponent[K, V](f: (V, V) => V) { var mem = Map[K, V]() def apply(kv: KeyVal[K, V]) = { val KeyVal(k, v) = kv mem

Why wrapping a generic method call with Option defers ClassCastException?

最后都变了- 提交于 2019-11-29 09:14:19
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 consider a following class: case class DummyRow() { val foo: Any = 1 : Int def getAs[T] = foo.asInstanceOf[T] def getAsOption[T] = Option(foo.asInstanceOf[T]) } As far as I can tell getAs should behave the same way as the previous apply followed by asInstanceOf . Surprisingly it is not the case. When called alone it throws an exception: DummyRow().getAs