[EDIT BELOW]
I have a class hierarchy that right now looks something like this:
sealed trait Foo {
def method: Any
}
case class Bar extends Foo {
def method: Array[String] =
}
case class Baz extends Foo {
def method: Map[String, Array[String]] =
}
I give the abstract method a return type Any, because the return types of the case classes are necessarily different, but they have a similar purpose. For this reason, I would like to keep this in the dash in order to simulate this usual behavior, and this is the only way I found to compile it. I understand that this is contrary to the spirit of a system like Scala, so I ask the first question below.
Then another class expects a subclass Fooas a constructor parameter, and I don’t know how to denote it except the following:
class Qux(foo: Foo) {
val m = foo.method
...
...
}
Qux , , val m , (Bar Baz) Foo,
... type mismatch;
[error] found : Any
[error] required: Array[String]
, :
- Scala, , , , , . , ?
- ,
Qux, m , method Bar Baz, Foo?
: , @marios ( ), , , . Qux
class Qux[X <: Foo](sc: SparkContext, val foo: X) {
val m: foo.A = foo.method
def process(rows: DataFrame) = foo match {
case Bar(sc, _) => BarProcessor(sc, m).doStuff(rows)
case Baz(sc, _) => BazProcessor(sc, m.keys).doStuff(rows, m.values)
}
}
BarProcessor , , Array[String], BazProcessor - , Baz method, -. ,
[error] Qux.scala:4: type mismatch;
[error] found : Qux.this.foo.A
[error] required: Array[String]
[error] case Bar(sc, _) => BarProcessor(sc, m).doStuff(rows)
[error] ^
, Map - m, Foo Baz ( value keys is not a member of Qux.this.foo.A ..). , m Array[String] - A. Scala "" ?