Skip to content

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

Closed
smarter opened this issue Nov 1, 2018 · 7 comments
Closed

Implicit conversion warning when converting an Int to a Double #5360

smarter opened this issue Nov 1, 2018 · 7 comments
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement

Comments

@smarter
Copy link
Member

smarter commented Nov 1, 2018

We probably should tone this down:

scala> val x: Double = (1: Int)
1 |val x: Double = (1: Int)
  |                 ^^^^^^
  |Use of implicit conversion method int2double in object Int should be enabled
  |by making the implicit value scala.language.implicitConversions visible.
  |This can be achieved by adding the import clause 'import scala.language.implicitConversions'
  |or by setting the compiler option -language:implicitConversions.
  |See the Scala docs for value scala.language.implicitConversions for a discussion
  |why the feature should be explicitly enabled.
val x: Double = 1.0

(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)

@smarter smarter added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc labels Nov 1, 2018
@smarter
Copy link
Member Author

smarter commented Nov 1, 2018

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, int2float is a terrible idea that should get dropped. @sjrd this seems like it's going to interact with the weak conformance SIP, what do you think ?

@sjrd
Copy link
Member

sjrd commented Nov 1, 2018

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.

@odersky
Copy link
Contributor

odersky commented Nov 2, 2018

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.
One easy way is to exempt everything that's defined in the scala package.

@ekrich
Copy link
Contributor

ekrich commented Dec 19, 2019

I am running into these warnings using JUnit using 0.21.0-RC1.

[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.

@SethTisue
Copy link
Member

SethTisue commented Apr 30, 2020

another example where the warning seems unnecessary:

scala> 0 #:: LazyList()                                                                             
1 |0 #:: LazyList()
  |      ^^^^^^^^^^
  |Use of implicit conversion method toDeferrer in object LazyList should be enabled
  |by adding the import clause 'import scala.language.implicitConversions'
  |or by setting the compiler option -language:implicitConversions.
  |See the Scala docs for value scala.language.implicitConversions for a discussion
  |why the feature should be explicitly enabled.
val res0: LazyList[Int] = LazyList(<not computed>)

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. #:: and #::: are just extension methods, there is no "true" implicit conversion here

@bvenners
Copy link

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.

@smarter
Copy link
Member Author

smarter commented Oct 5, 2020

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.

@smarter smarter closed this as completed Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement
Projects
None yet
Development

No branches or pull requests

6 participants