How does Scala's method reference work? Suppose there are the following lists:
val list12 = List(1, 2)
val list012 = 0 :: list1 //insert at head
val list123 = list1 :+ 3 //insert at end
Suppose there is a higher order function which accepts an element, a list, and also a function which adds the element to the list and returns the new list:
def append[T](e: T, list: List[T], fun: (List[T], T) => List[T]): List[T] = {
fun(list, e)
}
If it were to have such a method locally defined:
def appendAtHead[T](list: List[T], e: T): List[T] = {
e :: list
}
Then the call would be simple:
append(0, list12, appendAtHead[Int])
But how to reference the existing methods ::
and :+
? The following calls do not work (as they do in Java for example):
append(0, list12, List.::)
append(3, list12, SeqLike.:+)