question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typeable instance not being discovered on generic type typetest

See original GitHub issue

Compiler 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:open
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolasstuckicommented, Jun 3, 2021

Note that that warning has nothing to do with Typeable. It is just the consequence type erasure on classes.

1reaction
liufengyuncommented, Jun 1, 2021

Wondering if this is something that can be improved in the future?

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found