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
This "bug" came up in a Stackoverflow question.
When you put a subtype constraint on a type parameter in an implicit conversion, the conversion is also applied when an unrelated type is involved. My guess is that Nothing is involved here somewhere. But when I want to check the type with a TypeTag, the conversion no longer kicks in for the unrelated type, and for the right type I get "error: macro has not been expanded".
scala>traitTestBase
defined traitTestBase
scala>classNotTest
defined classNotTest
scala>caseclassFoo()
defined classFoo
scala>implicitdefconv[A<:TestBase](foo: Foo):A= {println(foo); null.asInstanceOf[A] }
warning: there were 1 feature warning(s); re-run with-feature for details
conv: [A<:TestBase](foo: Foo)A
scala>valtest:NotTest=Foo()
Foo()
test:NotTest=null
scala>implicitdefconv[A](foo: Foo)(implicitev: A<:<TestBase):A= {println(foo); null.asInstanceOf[A] }
warning: there were 1 feature warning(s); re-run with-feature for details
conv: [A](foo: Foo)(implicitev: <:<[A,TestBase])A
scala>valtest:NotTest=Foo()
Foo()
test:NotTest=null
scala>importscala.reflect.runtime.universe._importscala.reflect.runtime.universe._
scala>implicitdefconv[A<:TestBase](foo: Foo)(implicitev: TypeTag[A]):A= {println(ev); null.asInstanceOf[A] }
warning: there were 1 feature warning(s); re-run with-feature for details
conv: [A<:TestBase](foo: Foo)(implicitev: reflect.runtime.universe.TypeTag[A])A
scala>valtest:NotTest=Foo()
<console>:18:error: typemismatch;
found : Foorequired: NotTestvaltest:NotTest=Foo()
^
scala>valtest:TestBase=Foo()
<console>:17:error: macro has not been expanded
valtest:TestBase=Foo()
^
The text was updated successfully, but these errors were encountered:
@retronym said:
I'd suggest using -Xprint:typer to see what is inferred, rather than adding TypeTag-s. This might help you to untangle the issues better and determine if this is a bug or just a case of inference doing what it specced to do.
@szeiger said:
The first part works as expected (as -Xprint:typer will verify). The implicit conversion claims to be able to produce any subtype of TestBase, which includes Nothing. Since you need a cast to get there, it's not a soundness issue, either (but the similar #9633 is). You'd have to add a subtype of all the types the implicit conversion can really produce, and use that as an upper bound.
The fact that it doesn't work with a TypeTag looks like a legitimate bug to me. An explicit call conv(Foo()) compiles but conv is not picked as an implicit conversion.
This "bug" came up in a Stackoverflow question.
When you put a subtype constraint on a type parameter in an implicit conversion, the conversion is also applied when an unrelated type is involved. My guess is that Nothing is involved here somewhere. But when I want to check the type with a TypeTag, the conversion no longer kicks in for the unrelated type, and for the right type I get "error: macro has not been expanded".
The text was updated successfully, but these errors were encountered: