Implicit conversions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Implicit conversions

Michel Charpentier
import scala.concurrent.duration.Duration
import scala.concurrent.duration.SECONDS
import scala.language.implicitConversions

object 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.
Reply | Threaded
Open this post in threaded view
|

Re: Implicit conversions

som-snytt

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.Duration
import scala.concurrent.duration.SECONDS
import scala.language.implicitConversions

object 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.
Reply | Threaded
Open this post in threaded view
|

Re: Implicit conversions

Michel Charpentier
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,

MC

On 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.Duration
import scala.concurrent.duration.SECONDS
import scala.language.implicitConversions

object 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.