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

此生再无相见时 提交于 2019-12-18 10:05:36

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/1124099/what-is-the-meaning-of-colon-underscore-and-star-in-lifts-sitemapentries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!