Copy paste this code to a scala worksheet to view the results.

The final asquares method is fully generic both with collections and number types, you'll get this result :


asquares(Seq(1,2,3)) //> res1: Seq[Int] = List(1, 4, 9)
asquares(List(1,2,3)) //> res2: List[Int] = List(1, 4, 9)
asquares(Vector(BigDecimal(1),BigDecimal(2),BigDecimal(3)))
//> res3: Vector[BigDecimal] = Vector(1, 4, 9)
asquares(Array(1,2,3)) //> res4: Array[Int] = Array(1.0, 4.0, 9.0)
asquares(Set(2,4)) //> res5: Set[Int] = Set(4, 16)

The right types are always returned !