Skip to content

Implicit search fails for new Random.shuffle(List(1,2)) #553

Closed
@smarter

Description

@smarter

This compiles with scalac but not with dotty:

import scala.util.Random

object Test {
  def test = {
    val rand = new Random
    rand.shuffle(List(1,2)) // fails
  }
}
try/t6948b.scala:6: error: no implicit argument of type scala.collection.generic.CanBuildFrom[
  (CC?
     >: ([+HK$0] => scala.collection.immutable.List[HK$0])[HK$0 =+ (T? >: Int)]
       <: 
    ([HK$0] =>  <: scala.collection.TraversableOnce[HK$0])
  )[(T? >: Int)]#Apply
, (T? >: Int), 
  (CC?
     >: ([+HK$0] => scala.collection.immutable.List[HK$0])[HK$0 =+ (T? >: Int)]
       <: 
    ([HK$0] =>  <: scala.collection.TraversableOnce[HK$0])
  )[(T? >: Int)]#Apply
] found for parameter bf of method shuffle in class Random
    rand.shuffle(List(1,2))
                           ^
one error found

But explicitly doing an implicit search is enough to make it work:

import scala.util.Random
import scala.collection.generic._
import scala.collection.immutable._

object Test {
  implicit val z: CanBuildFrom[List[Int], Int, List[Int]] =
    implicitly[CanBuildFrom[List[Int], Int, List[Int]]] // succeeds

  def test = {
    val rand = new Random
    rand.shuffle(List(1,2)) // now succeeds
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions