Skip to content

Implicit conversions to Any/AnyVal/AnyRef should be disallowed #6336

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

Closed
OlivierBlanvillain opened this issue Apr 18, 2019 · 1 comment
Closed

Comments

@OlivierBlanvillain
Copy link
Contributor

OlivierBlanvillain commented Apr 18, 2019

According to the documentation on AnyVal, Strings are not value types, so I believe the following shouldn't compile:

val a: AnyVal = "foo"

Here is how scalac complains:

$ echo 'val a: AnyVal = ""' | scala
Welcome to Scala 2.13.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_201).
scala> val a: AnyVal = ""
                       ^
       error: the result type of an implicit conversion must be more specific than AnyVal
$ echo 'val a: AnyVal = ""' | scala -Yno-predef
Welcome to Scala 2.13.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_201).
scala> val a: AnyVal = ""
                       ^
       error: type mismatch;
        found   : String("")
        required: AnyVal
@odersky
Copy link
Contributor

odersky commented Apr 18, 2019

It inserts Predef.augmentString as an implicit conversion. Once we move to extension methods this problem is gone. Until then it would be good to bring back the restrictions that implicit conversions can't convert to Any, AnyVal, or AnyRef.

@smarter smarter changed the title Strings shouldn't be in AnyVal Implicit conversions to Any/AnyVal/AnyRef should be disallowed Apr 18, 2019
@smarter smarter closed this as completed in 33043bd Mar 6, 2020
smarter added a commit that referenced this issue Mar 6, 2020
Fix #6336: Error on implicit conversions to AnyVal or AnyRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants