$ scala -Xexperimental
Welcome to Scala version 2.9.0.1 (OpenJDK Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class D extends Dynamic {
| def invokeDynamic(s:String)(args:Any*) = println(s)
| def doo() = { this hello }
| }
dynatype: this.applyDynamic("hello")()
dynatype: this.applyDynamic("applyDynamic")
...
这会重复几十次......
...
dynatype: this.applyDynamic("applyDynamic")
java.lang.StackOverflowError
at scala.tools.nsc.symtab.Types$TypeMap$$anonfun$mapOverArgs$1.apply(Types.scala:3107)
at scala.tools.nsc.symtab.Types$TypeMap$$anonfun$mapOverArgs$1.apply(Types.scala:3103)
at scala.tools.nsc.symtab.Types$class.map2Conserve(Types.scala:4867)
at scala.tools.nsc.symtab.SymbolTable.map2Conserve(SymbolTable.scala:13)
at scala.tools.nsc.symtab.Types$TypeMap.mapOverArgs(Types.scala:3103)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3010)
at scala.tools.nsc.symtab.Types$ApproximateDependentMap$.apply(Types.scala:3594)
at scala.tools.nsc.symtab.Types$ApproximateDependentMap$.apply(Types.scala:3591)
at scala.tools.nsc.symtab.Types$TypeMap$$anonfun$16.apply(Types.scala:3125)
at scala.tools.nsc.symtab.Types$TypeMap$$anonfun$16.apply(Types.scala:3122)
at scala.collection.LinearSeqOptimized$class.exists(LinearSeqOptimized.scala:79)
at scala.collection.immutable.List.exists(List.scala:45)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3122)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3024)
at scala.tools.nsc.symtab.Types$ApproximateDependentMap$.apply(Types.scala:3594)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.scala$tools$nsc$typechecker$Implicits$ImplicitSearch$$depoly(Implicits.scala:261)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation.survives(Implicits.scala:619)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation$$anonfun$6$$anonfun$7.apply(Implicits.scala:648)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation$$anonfun$6$$anonfun$7.apply(Implicits.scala:648)
at scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:213)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.TraversableLike$class.filter(TraversableLike.scala:212)
at scala.collection.immutable.List.filter(List.scala:45)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation$$anonfun$6.apply(Implicits.scala:648)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation$$anonfun$6.apply(Implicits.scala:647)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:200)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:200)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:200)
at scala.collection.immutable.List.flatMap(List.scala:45)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch$ImplicitComputation.<init>(Implicits.scala:647)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:753)
at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.bestImplicit(Implicits.scala:1084)
at scala.tools.nsc.typechecker.Implicits$class.inferImplicit(Implicits.scala:57)
at scala.tools.nsc.Global$analyzer$.inferImplicit(Global.scala:347)
at scala.tools.nsc.typechecker.Typers$Typer.wrapImplicit$1(Typers.scala:167)
at scala.tools.nsc.typechecker.Typers$Typer.inferView(Typers.scala:171)
at scala.tools.nsc.typechecker.Typers$Typer.adaptToMember(Typers.scala:985)
at scala.tools.nsc.typechecker.Typers$Typer.adaptToMemberWithArgs(Typers.scala:1024)
at scala.tools.nsc.typechecker.Typers$Typer.typedSelect$1(Typers.scala:3534)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4123)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4217)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedApply$1$1.apply(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedApply$1$1.apply(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:623)
at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4062)
at scala.tools.nsc.typechecker.Typers$Typer.typedSelect$1(Typers.scala:3554)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4123)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4217)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedApply$1$1.apply(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$typedApply$1$1.apply(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:623)
at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:3326)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4062)
at scala.tools.nsc.typechecker.Typers$Typer.typedSelect$1(Typers.scala:3554)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4123)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4217)
at
依此类推...直到最后:
The repl compiler has crashed spectacularly. Shall I replay your
session? I can re-run all lines except the last one.
[y/n]
它会使编译器崩溃,因为它找不到正确的方法来处理您的代码。 我同意它不应该使编译器崩溃,而应该返回有用的错误消息。
请注意,使用正确的方法名称它可以正常工作
applyDynamic
:
scala> class D extends Dynamic {
def applyDynamic(s: String)(i: Int) {
println("Called "+ s +" with "+ i)
}
}
defined class D
scala> val d = new D
d: D = D@3b2601c
scala> d hello 42
dynatype: $line33.$read.$iw.$iw.d.applyDynamic("hello")
Called hello with 42
很奇怪。我认为它不应该编译,因为你应该实现
applyDynamic
而不是 invokeDynamic
。这个对我有用:
class D extends Dynamic {
def applyDynamic(s: String)(args: Any*) = println(s)
def doo() = { this.hello }
}
可能
invokeDynamic
以某种方式与合成材料混淆......?