You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Invocation of method foo with an integer argument returns 1:
scala>newC().foo(42)
res0:Int=1
Invocation of resolveOverload on the term symbol foo however throws a ClassCastExceptipon. The reason is, that resolveOverload currently only expects method symbols in the alternatives, but this is not guaranteed.
scala>importscala.reflect.runtime.universe._importscala.reflect.runtime.universe._
scala>importscala.reflect.runtime.{ currentMirror=>cm }
importscala.reflect.runtime.{currentMirror=>cm}
scala>classB {
|objectfoo {
|defapply(x: Int) =2| }
| }
defined classB
scala>classCextendsB {
|deffoo(x: Int) =1| }
defined classC
scala>valx=newC
x:C=C@3040c5
scala> x.foo(42)
res0:Int=1
scala>valt= typeOf[C]
t: reflect.runtime.universe.Type=C
scala>valfoo= t.member(newTermName("foo")).asTermSymbol
foo: reflect.runtime.universe.TermSymbol= value foo
scala> foo.alternatives.size
res1:Int=2
scala> foo.alternatives.map(_.isMethod)
res2:List[Boolean] =List(true, false)
scala> foo.alternatives.map(_.isModule)
res3:List[Boolean] =List(false, true)
scala>valfooSym= foo.resolveOverloaded(posVargs =List(typeOf[Int]))
java.lang.ClassCastException:objectfoo
at scala.reflect.base.Symbols$SymbolBase$class.asMethodSymbol(Symbols.scala:159)
at scala.reflect.internal.Symbols$SymbolContextApiImpl.asMethodSymbol(Symbols.scala:62)
at scala.reflect.internal.Symbols$SymbolContextApiImpl$$anonfun$21.apply(Symbols.scala:362)
at scala.reflect.internal.Symbols$SymbolContextApiImpl$$anonfun$21.apply(Symbols.scala:362)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:239)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:239)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:77)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:239)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at scala.reflect.internal.Symbols$SymbolContextApiImpl.resolveOverloaded(Symbols.scala:362)
at scala.reflect.internal.Symbols$TermSymbol.resolveOverloaded(Symbols.scala:2543)
at .<init>(<console>:15)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:736)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:991)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:579)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:610)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:574)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:742)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:787)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:699)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:563)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:570)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:573)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:864)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:819)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:819)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:819)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:79)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:92)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala){code}
In order to reproduce this bug use the following input:
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{ currentMirror => cm }
class B {
object foo {
def apply(x: Int) = 2
}
}
class C extends B {
def foo(x: Int) = 1
}
val x = new C
x.foo(42)
val t = typeOf[C]
val foo = t.member(newTermName("foo")).asTermSymbol
foo.alternatives.size
foo.alternatives.map(.isMethod)
foo.alternatives.map(.isModule)
val fooSym = foo.resolveOverloaded(posVargs = List(typeOf[Int]))
{code}
The text was updated successfully, but these errors were encountered:
@dgruntz said:
Actually, I wanted to define object foo as an extension of Int => Int, but in this case the result in the alternatives list is also an object.
Consider the following class with the overloaded symbol
foo
:Invocation of method
foo
with an integer argument returns 1:Invocation of
resolveOverload
on the term symbolfoo
however throws aClassCastExceptipon
. The reason is, thatresolveOverload
currently only expects method symbols in the alternatives, but this is not guaranteed.import scala.reflect.runtime.universe._
import scala.reflect.runtime.{ currentMirror => cm }
class B {
object foo {
def apply(x: Int) = 2
}
}
class C extends B {
def foo(x: Int) = 1
}
val x = new C
x.foo(42)
val t = typeOf[C]
val foo = t.member(newTermName("foo")).asTermSymbol
foo.alternatives.size
foo.alternatives.map(.isMethod)
foo.alternatives.map(.isModule)
val fooSym = foo.resolveOverloaded(posVargs = List(typeOf[Int]))
{code}
The text was updated successfully, but these errors were encountered: