私が達成しようとしていることを示すために、以下のような工夫した例を作成しました。私はそれが処理する特定のタイプのリクエストでパラメータ化されたクラスを吐き出すファクトリを探しています。私はジェネリックスを理解していると思ったが、今は自分自身を疑う。 :)でScala:ネストされたジェネリックスでタイプの不一致があります
object SourceFactory {
def apply[T <: Source[_ <: RequestContext]](x: String): Source[_ <: RequestContext] = {
x match {
case "a" => new FooSource
}
}
}
object RequestContextFactory {
def apply[T >: RequestContext](x: String): T = {
x match {
case "a" => FooRequestContext()
}
}
}
abstract class RequestContext
case class FooRequestContext() extends RequestContext
abstract class Source[T <: RequestContext] {
def get(ctx: T): Unit
}
class FooSource extends Source[FooRequestContext] {
def get(ctx: FooRequestContext): Unit = {}
}
object Test extends App {
val source = SourceFactory("a")
val ctx = RequestContextFactory("a")
source.get(ctx)
}
結果:助けを事前に
Compiler exception error: line 32: type mismatch; found : Evaluator__da15fb805d29b29227bc28ccae6cd07d2c04cb40_1274353927.Test.ctx.type (with underlying type RequestContext) required: _$2 source.get(ctx)
ありがとう!
、どうもありがとう! – richid