Jay Taylor's notes

back to listing index

scala - What is the meaning of colon, underscore and star in lift's SiteMap(entries:_*)? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: stackoverflow.com
Clipped on: 2012-08-13

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...

asked Jul 14 '09 at 8:06
Image (Asset 1/1) alt=849623

100% accept rate
  upvote
 flag
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
  upvote
 flag
Very nice question. I'd upvote it many times if I could! – Ivan Oct 11 '11 at 21:25
add comment

1 Answer

up vote 25 down vote accepted

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:

none

... 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.

none

I find the description offered here to be more helpful.

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

answered Jul 14 '09 at 8:31
Palimondo
849623
  upvote
 flag
Yep, that's it. -) – Daniel C. Sobral Jul 14 '09 at 14:03
  upvote
 flag
Thanks a lot, dude. I'd wish I could find more explanations of this kind here. – Ivan Oct 11 '11 at 21:24
  upvote
 flag
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 at 17:08
add comment

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.