Skip to content

Int convert to Double when unnecessary #1533

Closed
@XuefengWu

Description

@XuefengWu
  def f1(l: Array[Int]): Array[Any] =
       l.map {x =>
        if(x == 1) x.toString
        else if (x == 2) x.toInt
        else  x.toDouble
      }  

scala> f1(Array(1,2,3)).foreach(v => println(s"$v :${v.getClass}"))
1 :class java.lang.String
2.0 :class java.lang.Double  //this shoule be Int
3.0 :class java.lang.Double

the compiled code box the second result type is Double but not Int.

if use pattern match, it works well in dotty

def f2(l: Array[Int]): Array[Any] =
      l.map{ x =>
        x match {
        case x if x == 1 => x.toString
        case x if x == 2 => x.toInt
        case _ => x.toDouble
      }    
   }    

scala> f2(Array(1,2,3)).foreach(v => println(s"$v :${v.getClass}"))
1 :class java.lang.String
2 :class java.lang.Integer
3.0 :class java.lang.Double

but this also doest not works well in scalac 2.11/2.12

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