Gurupraveen
Gurupraveen

Reputation: 181

Scala List append operation - need explaination

Based on the explanation and my understanding on the List append operation in scala it is right associative. So given a statement List1 ::: List2 is nothing but List2.:::List1. With this said i tried to do following operations

val list1 = List(1,2,3,4) 
val list2 = List(5,6,7,8)  
list1 ::: list2 // this is good

list2.::: list1 //ERROR - Not applicable to List[B]code here

Why does not the second append operation compile?

Upvotes: 1

Views: 96

Answers (1)

NetanelRabinowitz
NetanelRabinowitz

Reputation: 1594

The reason it is not compile is because there's a mix and math with the syntax there. you can use the regular syntax for method invocation:

list2.:::(list1)

Notice the dot AND the parenthesis.

Or you can use the infix notation for invoking methods of arity-1:

list1 ::: list2

But you cant mix between the two.

The ::: operator is actually a prepend and not append operator, it operates on list2 (as you stated correctly it is right associative) and adds the elements of list1 in front of it.

Upvotes: 1

Related Questions