Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is invokeDynamic in a class extending Dynamic not working?

$ 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")
...

This repeats a few dozen times....

...
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 

And so on...until finally:

The repl compiler has crashed spectacularly. Shall I replay your
session? I can re-run all lines except the last one.
[y/n]
like image 286
Kim Stebel Avatar asked Jul 22 '11 23:07

Kim Stebel


1 Answers

It crashes the compiler because it can't find the right method to handle your code. I agree that it shouldn't crash the compiler, but should return an useful error message instead.

Note that it works correctly with the right method name 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
like image 116
soc Avatar answered Sep 29 '22 09:09

soc