Implicit def without import

I have Scala 2.13.3 installed and I want to use implicit def for type conversion.
In the docs I read that the following import is necessary:

import scala.language.implicitConversions

I use this import and everything works fine.

But even when I don’t use this import everything works fine.
So my question is, whether the import is really necessary and when?

If my memory don’t fails me here, than this import is no longer needed since Scala 2.13.1. Before that version a warning message would be emitted by the compiler.

Hello cbley,

thank you very much for the quick answer.
I didn’t know that this was changed in Scala 2.13.

The import was actually never needed. It is just like a kind of flag to warn people. Since implicit conversions are discouraged.

Without the import you will get a warning if you define an implicit conversion. AFAICT that’s still the case in the latest version of Scala. But it’s possible that that warning is silenced somehow in your setup if you’re trying it in a REPL, or that it’s already enabled for your entire project with a compiler flag or an sbt plugin.

1 Like

I don’t think it’s been removed. I just tried in the 2.13.3 REPL:

$ scala-2.13.3/bin/scala
Welcome to Scala 2.13.3 (OpenJDK 64-Bit Server VM, Java 14.0.1).
Type in expressions for evaluation. Or try :help.

scala> case class Dollars(amount: Double) {
| override def toString = f"$$$amount%8.2f"
| }
| case class Percentage(amount: Double) {
| assert(amount >= 0.0 && amount <= 1.0)
| override def toString = f"${(amount*100.0)}%3.2f%%"
| }
| implicit def toDollars(d:Double): Dollars = Dollars(d)
| implicit def toPercentage(d:Double): Percentage = Percentage(d)
implicit def toDollars(d:Double): Dollars = Dollars(d)
On line 14: warning: implicit conversion method toDollars 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 Scaladoc for value scala.language.implicitConversions for a discussion
why the feature should be explicitly enabled.
implicit def toPercentage(d:Double): Percentage = Percentage(d)
On line 15: warning: implicit conversion method toPercentage should be enabled
by making the implicit value scala.language.implicitConversions visible.

The same thing happens in Scala 2.13.1. 2.12.?, and also dotr.

Do you have the -language:implicitConversions flag defined somewhere?

I didn’t say it was removed, I just said it was actually never needed at all.

Bad wording on my part.

I now tried it in the REPL and there without the import I indeed get the warning as Dean
On the other hand in IntelliJ IDEA I don’t need the import to make “implicit def” work.
Perhaps it’s a feature of the Scala Plugin of IntelliJ (IntelliJ 2020.1.1 Ultimate Edition)

Maybe of interest: the Ammonite Repl does not require the imports and does not issue any warning. (Ammonite Repl 2.2.0 / Scala 2.13.3)

IntelliJ uses their own compiler instead of the presentation compiler, so maybe they do not even have that implemented.

As others pointed out, I was indeed wrong. The warning is still emitted by default; nothing changed between Scala versions. We simply enabled the -language:implicitConversions compiler option globally in all our projects and I forgot about that.