Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't infer type of Array.apply when ClassTag and implicit conversion to Seq are both involved #12920

Open
pengmaokui opened this issue Dec 7, 2023 · 1 comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) typer
Milestone

Comments

@pengmaokui
Copy link

pengmaokui commented Dec 7, 2023

Reproduction steps

Scala version: (2.13.12)

  import reflect.ClassTag
  def main(args: Array[String]): Unit = {

    test(Array("jan", "feb", "mar", "april", "may", "jun"))
  }

  def test[T: ClassTag](seq: Seq[T]): Unit = {
    println(seq)
  }

Problem

Compile not pass

overloaded method apply with alternatives:
  (x: Unit,xs: Unit*)Array[Unit] <and>
  (x: Double,xs: Double*)Array[Double] <and>
  (x: Float,xs: Float*)Array[Float] <and>
  (x: Long,xs: Long*)Array[Long] <and>
  (x: Int,xs: Int*)Array[Int] <and>
  (x: Char,xs: Char*)Array[Char] <and>
  (x: Short,xs: Short*)Array[Short] <and>
  (x: Byte,xs: Byte*)Array[Byte] <and>
  (x: Boolean,xs: Boolean*)Array[Boolean]
 cannot be applied to (String, String, String, String, String, String)
    test(Array("jan", "feb", "mar", "april", "may", "jun"))
@som-snytt
Copy link

Scala 3 infers String, but the copying conversion is deprecated.

scala 2.13.12> test(Array[String]("jan", "feb", "mar", "april", "may", "jun")) // print

test[String](scala.Predef.copyArrayToImmutableIndexedSeq[String](scala.Array.apply[scala.Predef.String]("jan", "feb", "mar", "april", "may", "jun")(scala.reflect.`package`.materializeClassTag[String]()))) // : (implicit evidence$1: scala.reflect.ClassTag[String]): Unit

If you take the advice and use toIndexedSeq, it will also infer String:

scala 2.13.12> test(Array[String]("jan", "feb", "mar", "april", "may", "jun")) // print
                                 ^
               warning: method copyArrayToImmutableIndexedSeq in class LowPriorityImplicits2 is deprecated (since 2.13.0): implicit conversions from Array to immutable.IndexedSeq are implemented by copying; use `toIndexedSeq` explicitly if you want to copy, or use the more efficient non-copying ArraySeq.unsafeWrapArray
ArraySeq(jan, feb, mar, april, may, jun)

scala 2.13.12> test(Array[String]("jan", "feb", "mar", "april", "may", "jun").toIndexedSeq)
ArraySeq(jan, feb, mar, april, may, jun)

scala 2.13.12> test(Array("jan", "feb", "mar", "april", "may", "jun").toIndexedSeq)
ArraySeq(jan, feb, mar, april, may, jun)

Of the overloads of Array.apply, the primitive versions are more specific than the generic.

Maybe Scala 3 defers inferring the type until after it finds the conversion. (Just a guess.)

@SethTisue SethTisue added typer fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) labels Jan 26, 2024
@SethTisue SethTisue added this to the Backlog milestone Jan 26, 2024
@SethTisue SethTisue changed the title overloaded method apply with alternatives Can't infer type of Array.apply when ClassTag and implicit conversion to Seq are both involved Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) typer
Projects
None yet
Development

No branches or pull requests

3 participants