Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does _ :: mean in Scala?

Tags:

scala

lift

I am going through a book Lift in action and I encountered something I don't quite understand: _ ::

object permanent_link extends MappedString(this, 150){
    override def validations =
    valMinLen(3, "Link URL must be at least 5 characters") _ ::
    super.validations
}

I can't find any hint so I would be grateful if anyone could help me out..

like image 244
Jarek Avatar asked Jan 10 '12 18:01

Jarek


2 Answers

I don't know Lift, but this is a general question. First of all, the :: is a Scala cons operator:

scala> 1 :: 2 :: List(3, 4)
res0: List[Int] = List(1, 2, 3, 4)

This means that super.validations is some sort of a sequence and valMinLen(3, "Link URL must be at least 5 characters") _ is a single value in that list.

From the context it looks obvious that in overridden validations method they are calling super version and prepend some extra validation at the beginning.

This extra validation is created by a call to valMinLen(). However this extra call does not return an element matching the type of validations list - but a function. Instead of prepending the function value we are explicitly saying (by adding _ suffix`) that we want to prepend a function itself, not a return value of that function.

Code snippet is worth a thousand words:

scala> def f = 3
f: Int

scala> def g = 4
g: Int

scala> val listOfInts = List(f, g)
listOfInts: List[Int] = List(3, 4)

scala> val listOfFunctions = List(f _, g _)
listOfFunctions: List[() => Int] = List(<function0>, <function0>)

Compare the type of listOfInts and listOfFunctions. I believe the f _ syntax is called partially applied function in Scala world.

like image 176
Tomasz Nurkiewicz Avatar answered Nov 08 '22 13:11

Tomasz Nurkiewicz


The underscore ignifies that valMinLen isn't to be called but be used as a function "pointer".

The :: operator concatenates lists.

It would in other words seem like the code builds a list validations that consist of a function "pointer" to valMinLen with the parameters given and the rest of the list is the value of super.validations, that is the super class' validations.

I'm sure someone will correct my terminology here :)

like image 20
Joachim Isaksson Avatar answered Nov 08 '22 12:11

Joachim Isaksson