When searching for implicit conversion, does Scala use the destination type? -


i'm reading book scala in depth, chapter 5 implicits. author says on page 102:

the implicit scope used implicit views same implicit parameters. when compiler looking type associations, uses type it's attempting convert from [my emphasis], not type it's attempting convert to.

and yet, few pages later shows example, complexmath.complexnumber class. import i, complexnumber, , call it's * method, takes complexnumber argument.

import complexmath.i * 1.0 

to convert 1.0 complexnumber, finds implicit conversion defined so:

package object complexmath {   implicit def realtocomplex(r: double) = new complexnumber(r, 0)   val = complexnumber(0, 1)     

but contradicts first statement, no? needed find double => complexnumber. why did in complexmath package, part of implicit scope complexnumber not double?

either source or target works:

object foo {   implicit def bar(b: bar): foo = new foo {}   implicit def foo(f: foo): bar = new bar {} } trait foo trait bar  implicitly[foo => bar]  // ok implicitly[bar => foo]  // ok  val b = new bar {} val bf: foo = b  // ok val f = new foo {} val fb: bar = f  // ok 

so think sentence wrong (?)


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -