-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit conversions with singleton type parameters are not checked like they are in Scala2 #14040
Comments
Scala 3's behavior is correct here. I believe it has to do with the fact that Scala 2 widen's types more eagerly than Scala 3 does. |
If you want to limit the conversions to constants, you can use a typeclass constraint: implicit def f[T <: String with Singleton: ValueOf](s: T): Int = s.length |
@odersky In the example I am explicity annotating |
@odersky Anyway, this issue can be reproduced even when scala> implicit def f(s: String with Singleton): Int = s.length
there were 1 feature warning(s); re-run with -feature for details
def f(s: String & Singleton): Int
scala> val notAConstant: String = "123" + java.lang.Math.random()
val notAConstant: String = 1230.1577068006814316
scala> notAConstant: Int
val res0: Int = 21
scala> def foo(x: String): Int = x: Int
def foo(x: String): Int
scala> foo("lol")
val res1: Int = 3
scala> foo("xyz")
val res2: Int = 3 Unless "singleton" has some other meaning that's not "literal constant"? That's what I thought it's meant to mean |
I think it means just
which you can't do with an unstable path. |
Singleton means a singleton type, the type of |
(For the exact definition see https://www.scala-lang.org/files/archive/spec/2.13/03-types.html#singleton-types) |
Compiler version
Minimized code
Dotty behavior:
Scala 2 behavior:
Expectation
I expect both Scala2 and Scala3 snippets to have the same behavior. Bumped into this in the context of com-lihaoyi/fansi#42
The text was updated successfully, but these errors were encountered: