Tell me if I don't know how to construct Lists
som-snytt opened this issue · comments
Inspired by https://issues.scala-lang.org/browse/SI-9851
Sometimes we lose track of brackets or forget to flatten. Can you help us?
More of these cases could warn that I did something untoward:
$ scala -Xlint
Welcome to Scala 2.12.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_92).
Type in expressions for evaluation. Or try :help.
scala> val b = List[List[Int]](List(1),List())
b: List[List[Int]] = List(List(1), List())
scala> b :+ List()
res0: List[List[Int]] = List(List(1), List(), List())
scala> b :: List(1)
<console>:13: warning: a type was inferred to be `Any`; this may indicate a programming error.
b :: List(1)
^
res1: List[Any] = List(List(List(1), List()), 1)
scala> b :: List()
res2: List[List[List[Int]]] = List(List(List(1), List()))
scala> b +: List()
res3: List[List[List[Int]]] = List(List(List(1), List()))
It might make a nice Abide rule to check for prepending lists to Nil or List(), where the type is inferred.
scala> List(1,2,3) :: List.empty[List[Int]]
res10: List[List[Int]] = List(List(1, 2, 3))
scala> List(1,2,3) :: List(List(4,5,6))
res11: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))
scala> List(1,2,3) :: List[List[Int]]()
res12: List[List[Int]] = List(List(1, 2, 3))
Abide is superseded by https://github.com/scalacenter/scalafix; closing all issues