0

The following conversion works because of int2double implicit conversion

scala> val d: Double = 2
d: Double = 2.0

prior to 2.10, this implicit conversion was part of Predef object and was thus always in scope. However since 2.10, it has been moved to Int companion object. How does the implicit conversion still work without any import?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66

1 Answers1

2

For implicits, besides the local scope (including imported ones), there is the implicit scope

Where does Scala look for implicits?

https://www.scala-lang.org/files/archive/spec/2.11/07-implicits.html

The actual arguments that are eligible to be passed to an implicit parameter of type fall into two categories. First, eligible are all identifiers that can be accessed at the point of the method call without a prefix and that denote an implicit definition or an implicit parameter. An eligible identifier may thus be a local name, or a member of an enclosing template, or it may be have been made accessible without a prefix through an import clause. If there are no eligible identifiers under this rule, then, second, eligible are also all implicit members of some object that belongs to the implicit scope of the implicit parameter's type, .

The implicit scope of a type consists of all companion modules of classes that are associated with the implicit parameter's type. Here, we say a class is associated with a type if it is a base class of some part of .

The parts of a type are:

  • ...

  • if is a parameterized type [1,…,], the union of the parts of and 1,…,;

  • ...

Implicit conversion Int => Double is Function1[Int, Double], so companion objects of Function1, Int, Double will be investigated.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66