# Implicit conversions

3 messages
Open this post in threaded view
|

## Implicit conversions

 `import scala.concurrent.duration.Durationimport scala.concurrent.duration.SECONDSimport scala.language.implicitConversionsobject Test extends App {  implicit def intsToDurations(l: List[Int]): List[Duration] =    for (n <- l) yield Duration(n, SECONDS)  val l = List(1, 2)  def f(l: List[Duration]) {    println(l)  }  f(l)  f(List(1,2): List[Int])  //f(List(1,2))}`The first 2 calls to f work fine, but the third is rejected.  Why is that?MC -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
 You can use -Ytyper-debug to see what it tries.It knows you need List[Duration], so the List.apply must produce it, so you're calling List.apply[Duration].Since that takes Durations, it looks for conversions from Int => Duration.The expected type exerts downward pressure on inference.You can also write f(List[Int](1,2)) to help it out.On Sunday, November 13, 2016 at 4:01:14 PM UTC-8, Michel Charpentier wrote:`import scala.concurrent.duration.Durationimport scala.concurrent.duration.SECONDSimport scala.language.implicitConversionsobject Test extends App {  implicit def intsToDurations(l: List[Int]): List[Duration] =    for (n <- l) yield Duration(n, SECONDS)  val l = List(1, 2)  def f(l: List[Duration]) {    println(l)  }  f(l)  f(List(1,2): List[Int])  //f(List(1,2))}`The first 2 calls to f work fine, but the third is rejected.  Why is that?MC -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
 Makes sense.  I completely forgot I was going through a parametrized apply method.  This also explains the error, which never complained about the type of the list but only the type of the list elements.Thanks,MCOn Sunday, November 13, 2016 at 11:27:56 PM UTC-5, som-snytt wrote:You can use -Ytyper-debug to see what it tries.It knows you need List[Duration], so the List.apply must produce it, so you're calling List.apply[Duration].Since that takes Durations, it looks for conversions from Int => Duration.The expected type exerts downward pressure on inference.You can also write f(List[Int](1,2)) to help it out.On Sunday, November 13, 2016 at 4:01:14 PM UTC-8, Michel Charpentier wrote:`import scala.concurrent.duration.Durationimport scala.concurrent.duration.SECONDSimport scala.language.implicitConversionsobject Test extends App {  implicit def intsToDurations(l: List[Int]): List[Duration] =    for (n <- l) yield Duration(n, SECONDS)  val l = List(1, 2)  def f(l: List[Duration]) {    println(l)  }  f(l)  f(List(1,2): List[Int])  //f(List(1,2))}`The first 2 calls to f work fine, but the third is rejected.  Why is that?MC -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.