Typeable instance not being discovered on generic type typetest
See original GitHub issueCompiler version
3.0.0
and nightly 3.0.1-RC1-bin-SNAPSHOT
Minimized code
import scala.reflect.Typeable
case class Env[T: Typeable](inner: T)
given [T](using Typeable[T]): Typeable[Env[T]] = new:
def unapply(x: Any): Option[x.type & Env[T]] = x match
case env: Env[?] =>
env.inner match
case _: T => Some(x.asInstanceOf[x.type & Env[T]])
case _ => None
case _ => None
@main def main =
val env: Env[?] = Env("test")
env match
case _: Env[Int] => println("Got Env[Int]")
case _: Env[String] => println("Got Env[String]")
case _ => println("Got Others")
Scastie: https://scastie.scala-lang.org/kacLqqBERuaF7qZ4vN2LTA
Output
the type test for Env[Int] cannot be checked at runtime
the type test for Env[String] cannot be checked at runtime
Got Env[Int]
Expectation
Got Env[String]
No warnings.
@liufengyun this appears to a bug?
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Should runtime type test without Typeable be a compiler error?
This got me thinking, if type testing like the code linked in the issue here: Typeable instance not being discovered on generic type...
Read more >Haskell Typeable instance - Stack Overflow
I can pass these types directly in arguments with "Max" "Min" "Moy" "Med" by deriving the Function datatype in the classes Data and...
Read more >A Reflection on Types
One particular class that we did not anticipate, although it made an early appearance in 1990 , was Typeable. The Typeable class gives...
Read more >A quick yet in-depth tour of TypeScript and its types - Medium
Union types, generics, JSX, type system loopholes and more! ... ZeroSize in this example, so they do not have a “current object”.
Read more >A tour of the Dart language
In the code above, number is inferred to be of type int . If you enable null safety, ... Note: Instance variables can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Note that that warning has nothing to do with
Typeable
. It is just the consequence type erasure on classes.I think for type applications where the constructor is an abstract type, there might be a possibility.
For classes, it’s difficult to specify when we should try the implicit search and it can be expensive if we always give it a try.
@nicolasstucki may have more thoughts on this.