Skip to content

Type parameter inference is too eager to widen union types #4867

Closed
@smarter

Description

@smarter

From https://stackoverflow.com/questions/51575183/dotty-seq-mapping-to-union:

object UnionMapping {
  private def parse(string: String): Int | Double = {
    if(string.contains("."))
      string.toDouble
    else
      string.toInt
  }

  def test_number = {
    val strings: Seq[String] = Seq("123", "2.0", "42")
    // Works
    val asdf: Seq[AnyVal] = strings.map(parse(_))
    // Fails to compile
    val union: Seq[Int | Double] = strings.map(parse(_))
  }
}

There's enough hints here that type inference should be able to do the right thing and not widen Int | Double to AnyVal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions