Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by a type that denotes an implicit conversion

Tags:

scala

implicit

The scala language specification, section 7.2 is about implicit scope: It explains that implicit scope are the modules associated in some way with the parts of type T. What the parts of T are is listed below. One of those points is

if T denotes an implicit conversion to a type with a method with argument types T1,…,Tn and result type U, the union of the parts of T1,…,Tn and U;

I can't make head or tails from this. I don't understand how a type T can denote an implicit conversion.

What is meant by this part of the specification of implicit scope?

like image 397
Martijn Avatar asked Feb 14 '20 15:02

Martijn


1 Answers

I believe this is referring to the following situation

case class Foo(v: Int)
object Foo {
  implicit def stringToFoo(s: String) = Foo(42)
}

def f[A](v: A)(implicit ev: A => Foo) = ev(v)

f("woohoo")

where the implicit conversion type T = A => Foo, and Foo is the part associated with type parameter A, therefore object Foo becomes part of the implicit scope and stringToFoo implicit conversion is resolved without needing an import.

like image 146
Mario Galic Avatar answered Sep 30 '22 03:09

Mario Galic