2012-09-16 54 views
1

當我需要從具有完全相同類型的類結果(不允許有差異)返回時,我在scala中使用遞歸類型。在scala中爲遞歸類型指定泛型的正確方法

我遇到了將此模式與其他結構集成的一些問題。

提供例如:

object Test { 
    trait Recursive[R <: Recursive[R]] 
    case class RecInt(data : Int) extends Recursive[RecInt] 
    case class RecFloat(data : Float) extends Recursive[RecFloat] 

    final case class ListHolder(rec : List[Recursive[_]]) { 
    def get[R <: Recursive[R]](index : Int) : R = rec(index).asInstanceOf[R] 
    } 
    val lh = ListHolder(List(RecInt(1), RecFloat(1.5f))) 
    val l0 : RecInt = lh.get(0) 

    final case class SingleHolder (rec : Recursive[_]) { 
    def get[R <: Recursive[R]] : R = rec.asInstanceOf[R] 
    } 
} 

給出錯誤輸出

/home/ayvango/Work/scala/test/RecursiveContainer.scala:12: error: type arguments [_$2] do not conform to trait Recursive's type parameter bounds [R <: Main.$anon.Test.Recursive[R]] 
    final case class SingleHolder (rec : Recursive[_]) { 
      ^
one error found 

錯誤描述非常清楚。

我應該如何指定類型邊界以符合要求?


更新:

我需要隱藏類型信息。我可能只使用長度爲1的列表來隱藏它。但應相當於該

我已經試過這種形式:

final case class SingleHolder (rec : Recursive[T] forSome {type T <: Recursive[T]}) { 
    def get[R <: Recursive[R]] : R = rec.asInstanceOf[R] 
} 

,但得到這個錯誤:

error: scala.tools.nsc.symtab.Types$TypeError: type mismatch; 
found : Test.Recursive[(some other)_1(in method equals)] where type (some other)_1(in method equals) <: Test.Recursive[_0] 
required: Test.Recursive[_ <: Test.Recursive[_0]] 
     at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:298) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:207) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.typeError(Infer.scala:217) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.typeErrorTree(Infer.scala:232) 
     at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:936) 
     at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:719) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4282) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:2191) 
     at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedArgs$3.apply(Typers.scala:2204) 
     at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedArgs$3.apply(Typers.scala:2203) 
     at scala.Tuple3$Zipped$$anonfun$map$1.apply(Tuple3.scala:66) 
     at scala.Tuple3$Zipped$$anonfun$map$1.apply(Tuple3.scala:64) 
     at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) 
     at scala.collection.immutable.List.foreach(List.scala:76) 
     at scala.Tuple3$Zipped.map(Tuple3.scala:64) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedArgs(Typers.scala:2203) 
     at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:2423) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:3373) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4106) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4273) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4333) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$5.apply(ExplicitOuter.scala:400) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$5.apply(ExplicitOuter.scala:387) 
     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233) 
     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233) 
     at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) 
     at scala.collection.immutable.List.foreach(List.scala:76) 
     at scala.collection.TraversableLike$class.map(TraversableLike.scala:233) 
     at scala.collection.immutable.List.map(List.scala:76) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.matchTranslation(ExplicitOuter.scala:387) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:511) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformTrees$1.apply(Trees.scala:873) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformTrees$1.apply(Trees.scala:873) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTrees(Trees.scala:873) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:837) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$5.apply(Trees.scala:783) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$5.apply(Trees.scala:781) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:780) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:480) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:797) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:460) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:875) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:767) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:766) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:765) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:797) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:460) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:875) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:767) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:766) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:765) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$1.apply(Trees.scala:761) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$1.apply(Trees.scala:761) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:760) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$2.apply(TypingTransformers.scala:51) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$2.apply(TypingTransformers.scala:51) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:51) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:892) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.scala$tools$nsc$transform$ExplicitOuter$ExplicitOuterTransformer$$super$transformUnit(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$transformUnit$1.apply(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$transformUnit$1.apply(ExplicitOuter.scala:529) 
     at scala.tools.nsc.symtab.SymbolTable.atPhase(SymbolTable.scala:95) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transformUnit(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transformUnit(ExplicitOuter.scala:318) 
     at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:30) 
     at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:329) 
     at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297) 
     at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297) 
     at scala.collection.Iterator$class.foreach(Iterator.scala:772) 
     at scala.collection.mutable.ListBuffer$$anon$1.foreach(ListBuffer.scala:318) 
     at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:297) 
     at scala.tools.nsc.transform.ExplicitOuter$Phase.run(ExplicitOuter.scala:539) 
     at scala.tools.nsc.Global$Run.compileSources(Global.scala:953) 
     at scala.tools.nsc.Global$Run.compile(Global.scala:1041) 
     at scala.tools.nsc.Main$.process(Main.scala:106) 
     at scala.tools.nsc.Main$.main(Main.scala:123) 
     at scala.tools.nsc.Main.main(Main.scala) 

Exception in thread "main" scala.tools.nsc.symtab.Types$TypeError: type mismatch; 
found : Test.Recursive[(some other)_1(in method equals)] where type (some other)_1(in method equals) <: Test.Recursive[_0] 
required: Test.Recursive[_ <: Test.Recursive[_0]] 
     at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:298) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:207) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.typeError(Infer.scala:217) 
     at scala.tools.nsc.typechecker.Infer$Inferencer.typeErrorTree(Infer.scala:232) 
     at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:936) 
     at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:719) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4282) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:2191) 
     at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedArgs$3.apply(Typers.scala:2204) 
     at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedArgs$3.apply(Typers.scala:2203) 
     at scala.Tuple3$Zipped$$anonfun$map$1.apply(Tuple3.scala:66) 
     at scala.Tuple3$Zipped$$anonfun$map$1.apply(Tuple3.scala:64) 
     at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) 
     at scala.collection.immutable.List.foreach(List.scala:76) 
     at scala.Tuple3$Zipped.map(Tuple3.scala:64) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedArgs(Typers.scala:2203) 
     at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:2423) 
     at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:3373) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4106) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4273) 
     at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4333) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$5.apply(ExplicitOuter.scala:400) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$5.apply(ExplicitOuter.scala:387) 
     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233) 
     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:233) 
     at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) 
     at scala.collection.immutable.List.foreach(List.scala:76) 
     at scala.collection.TraversableLike$class.map(TraversableLike.scala:233) 
     at scala.collection.immutable.List.map(List.scala:76) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.matchTranslation(ExplicitOuter.scala:387) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:511) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformTrees$1.apply(Trees.scala:873) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformTrees$1.apply(Trees.scala:873) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTrees(Trees.scala:873) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:837) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$5.apply(Trees.scala:783) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$5.apply(Trees.scala:781) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:780) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:480) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:797) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:460) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:875) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:767) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:766) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:765) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:797) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$1.apply(TypingTransformers.scala:49) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:460) 
     at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:875) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:767) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$2.apply(Trees.scala:766) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:765) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:891) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transformStats$1.apply(Trees.scala:889) 
     at scala.collection.immutable.List.loop$1(List.scala:148) 
     at scala.collection.immutable.List.mapConserve(List.scala:164) 
     at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:889) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$1.apply(Trees.scala:761) 
     at scala.tools.nsc.ast.Trees$Transformer$$anonfun$transform$1.apply(Trees.scala:761) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:760) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.scala$tools$nsc$transform$TypingTransformers$TypingTransformer$$super$transform(TypingTransformers.scala:49) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$2.apply(TypingTransformers.scala:51) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer$$anonfun$transform$2.apply(TypingTransformers.scala:51) 
     at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:899) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:38) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.atOwner(TypingTransformers.scala:31) 
     at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:51) 
     at scala.tools.nsc.transform.ExplicitOuter$OuterPathTransformer.transform(ExplicitOuter.scala:257) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transform(ExplicitOuter.scala:521) 
     at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:892) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.scala$tools$nsc$transform$ExplicitOuter$ExplicitOuterTransformer$$super$transformUnit(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$transformUnit$1.apply(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer$$anonfun$transformUnit$1.apply(ExplicitOuter.scala:529) 
     at scala.tools.nsc.symtab.SymbolTable.atPhase(SymbolTable.scala:95) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transformUnit(ExplicitOuter.scala:529) 
     at scala.tools.nsc.transform.ExplicitOuter$ExplicitOuterTransformer.transformUnit(ExplicitOuter.scala:318) 
     at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:30) 
     at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:329) 
     at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297) 
     at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297) 
     at scala.collection.Iterator$class.foreach(Iterator.scala:772) 
     at scala.collection.mutable.ListBuffer$$anon$1.foreach(ListBuffer.scala:318) 
     at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:297) 
     at scala.tools.nsc.transform.ExplicitOuter$Phase.run(ExplicitOuter.scala:539) 
     at scala.tools.nsc.Global$Run.compileSources(Global.scala:953) 
     at scala.tools.nsc.Global$Run.compile(Global.scala:1041) 
     at scala.tools.nsc.Main$.process(Main.scala:106) 
     at scala.tools.nsc.Main$.main(Main.scala:123) 
     at scala.tools.nsc.Main.main(Main.scala) 

我珍玩我怎麼可以模擬相同的行爲與列表

回答

0

身體的話題是有限的,我在這裏發佈下一個更新我的搜索:


UPDATE2:

現在我用這個恥辱建設:

final case class TypeErasure[T](data : T) 

final case class SingleHolder (rec : TypeErasure[Recursive[_]]) { 
    def get[R <: Recursive[R]] : R = rec.data.asInstanceOf[R] 
}