-1

Is there a relationship between implicit conversions and implicit parameters in Scala?

I have no interest what so ever in knowing what the different types of implicits are or what their resolution rules are.

I am only interested in why these two concepts have the same name.

eliasah
  • 39,588
  • 11
  • 124
  • 154
Michael Lafayette
  • 2,972
  • 3
  • 20
  • 54
  • 3
    This answers the question: http://stackoverflow.com/questions/5598085/where-does-scala-look-for-implicits – Giovanni Caporaletti Apr 09 '16 at 07:20
  • @GiovanniCaporaletti How does that answer the question? – Michael Lafayette Apr 09 '16 at 07:27
  • @MichaelLafayette It explains in detail what an implicit conversion and an implicit parameter are and how they are managed by the compiler. I honestly don't know what else could be added to that. It also puts everything in context with view bounds and context bounds, it's a comprehensive answer. – Giovanni Caporaletti Apr 09 '16 at 07:36
  • @MichaelLafayette implicit parameter requires implicit value or implicit conversion to convert from A to B where B is the implicit parameter required by the function. I think that answer explains pretty well in that regard. – Daniel Shin Apr 09 '16 at 07:38
  • @GiovanniCaporaletti I'm sorry I was distracted by the vast amount of text related to where one would search for implicits. Can the information that has nothing to do with "Where does Scala look for implicits" be moved to a separate question. With detailed examples focused on just "the relationship between implicit conversions and implicit parameters in Scala?" – Michael Lafayette Apr 09 '16 at 07:42

2 Answers2

3

An implicit conversion from type A to type B is an implicit function A => B.

An implicit def makes an implicit function available:

// two types
class A
class B
// implicit conversion from A to B
implicit def aToB(a: A): B = {
  println("aToB");
  new B
}
// we now have a Function[A, B] aka A => B in the implicit scope
val f = implicitly[A => B]
f(new A)
> aToB
res1: B = B@51931956

An implicit function enables implicit conversions:

class IntExtension(x: Int) { def isPositive = x > 0 }
implicit val intToIntExtensions: Int => IntExtension = x => new IntExtension(x)
> 1.isPositive
res2: Boolean = true
Rüdiger Klaehn
  • 12,445
  • 3
  • 41
  • 57
  • Let me see if I understand you correctly. implicitly[A => B] will return a reference to aToB. You can give this reference a new A and it will return a new B. The implicit val serves the same function as an implicit def. But what about a function with implicit parameters such as foo(int: Int)(implicit bar: Bar)? – Michael Lafayette Apr 09 '16 at 07:05
3

AFAIK, there isn't any immediate relationship except they share the same keyword.

But they can be combined in an interesting way like:

class Foo
class Bar
class FooBar(foo: Foo, bar: Bar)

implicit val foo = new Foo
implicit val bar = new Bar

implicit def fooBar(implicit foo: Foo, bar: Bar): FooBar = new FooBar(foo, bar)

implicitly[FooBar]

implicit conversion (fancy name, but really nothing more than an implicit that accepts parameters) can accept implicit parameters to act pretty much like an implicit val (given implicits are defined). Boom.

Daniel Shin
  • 5,086
  • 2
  • 30
  • 53