scala – cons operator ::

Because it ends in a colon, the :: (cons) operation associates to the right: A :: B :: C is interpreted as A :: (B :: C). Therefore, you can drop the parentheses in the previous definitions.

 

val v1: List[Int] = List(1, 2, 3)
val v1_1: List[Int] = 1 :: (2 :: (3 :: Nil))

val v1_2: List[Int] = 1 :: 2 :: 3 :: Nil


println(v1)
println(v1_1)
println(v1_2)

Leave a comment