-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implicit conversion warning when converting an Int to a Double #5360
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
Comments
So Int.scala contains: /** Language mandated coercions from Int to "wider" types. */
import scala.language.implicitConversions
implicit def int2long(x: Int): Long = x.toLong
implicit def int2float(x: Int): Float = x.toFloat
implicit def int2double(x: Int): Double = x.toDouble Can we get rid of those and just hardcode the behavior we want in the compiler ? At the very least, |
No it's independent of weak conformance. The implicit conversions would only kick in if there is an expected type that does not accept the existing terms. So the implicit conversions apply only when weak conformance does not. |
I think the implicit conversions are fine. They are defined as such. So the best way to implement them is as they are defined; we just need to teach the fact that these are good to the diagnostics. |
I am running into these warnings using JUnit using [warn] -- Feature Warning: /Users/eric/workspace/sconfig/sconfig/jvm/src/test/scala/org/ekrich/config/impl/ConcatenationTest.scala:162:31
[warn] 162 | assertEquals(4, conf.getInt("a.b"))
[warn] | ^^^^^^^^^^^^^^^^^^
[warn] |Use of implicit conversion method int2long in object Int should be enabled
[warn] |by adding the import clause 'import scala.language.implicitConversions'
[warn] |or by setting the compiler option -language:implicitConversions.
[warn] |See the Scala docs for value scala.language.implicitConversions for a discussion
[warn] |why the feature should be explicitly enabled. |
another example where the warning seems unnecessary:
this would go away under Martin's proposed "exempt everything that's defined in the scala package" rule, of course, but that's a big hammer. |
We got the warning just now when upgrading some tests that use ShouldMatchers, because we never changed the implicit conversion to an implicit class. (The creation of that implicit conversion to add a should method to Any predated the addition of implicit classes to Scala.) We will solve this in ScalaTest by finally getting around to changing those old implicit conversions to implicit classes and eventually in Dotty, extension methods. But it does seem to me to force folks into adding the feature import to use implicit conversions provided by a library was a bit cumbersome. If I make a Rational class, and make a conversion from Int to Rational, seems like that should just work. Perhaps this warning only happens if you use an implicit conversion that is in scope, not in companion objects. If so then that's not as cumbersome, but still, not sure it is worth it. |
Fixed by #9935, further discussion can go in https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388 or a new thread on contributors. |
We probably should tone this down:
(Note that this cannot be observed in the dotty project itself because we globally enable
-language:implicitConversions
, which is another issue since it means we're blind to the pain these warnings cause)The text was updated successfully, but these errors were encountered: