60

I'm learning Scala and lift at the same time and I got stuck on understanding the syntax used to inintialize the SiteMap in the Boot.scala:

 val entries = Menu(Loc("Home", "/", "Home")) :: 
       Menu(Loc("Foo", "/badger", "Foo")) ::
       Menu(Loc("Directory Foo", "/something/foo", "Directory Foo")) :: Nil 
 LiftRules.setSiteMap(SiteMap(entries:_*))

What exactly is the meaning of the SiteMap parameter? I see that the value entries is a list of Menu. What is the colon, underscore, star? At first I thought it is a method on the List, but I am unable to find such definition...

Palimondo
  • 7,281
  • 4
  • 39
  • 58
  • It's actually a pretty good question. I bet a lot of people face the same problem, which was, as you pointed out in your anwer, exarcebated by the lack of space between : and "_". – Daniel C. Sobral Jul 14 '09 at 14:04

1 Answers1

78

OK, after my colleague mentioned to me, that he encountered this secret incantation in the Programming in Scala book, I did a search in my copy and found it described in Section 8.8 Repeated parameters. (Though you need to search with space between the colon and underscore :-/ ) There is a one sentence to explain it as:

... append the array argument with a colon and an _* symbol, like this: scala> echo(arr: _*)

This notation tells the compiler to pass each element of arr as its own argument to echo, rather than all of it as a single argument.

I find the description offered here more helpful.

So x: _* is like a type declaration that tells the compiler to treat x as repeated parameter (aka variable-length argument list — vararg).

Palimondo
  • 7,281
  • 4
  • 39
  • 58
  • 1
    Thanks a lot, dude. I'd wish I could find more explanations of this kind here. – Ivan Oct 11 '11 at 21:24
  • 4
    So if you're familiar with Python, calling a function with *var does something similar: `var=(1, 2, 3) def f(*args): for arg in args: print arg f(*var)` That will print: 1 2 3 So apparently `x: _*` is Scala's equivalent to this. – Alan LaMielle Feb 15 '12 at 17:08