gpt4 book ai didi

scala - lift站点地图中冒号、下划线和星号的含义是什么(条目 :_*)?

转载 作者:行者123 更新时间:2023-12-03 05:44:33 25 4
gpt4 key购买 nike

我正在学习Scalalift与此同时,我陷入了理解用于初始化 SiteMap 的语法上。在 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:_*))

SiteMap 参数的具体含义是什么?我看到值 entries 是菜单列表。什么是冒号、下划线、星号?起初我以为这是List上的一个方法,但我找不到这样的定义......

最佳答案

好的,经过我的同事向我提及,他在 Programming in Scala 中遇到了这个 secret 咒语。书中,我在我的副本中进行了搜索,发现它在第 8.8 节重复参数中进行了描述。 (虽然搜索时需要冒号和下划线之间有空格:-/)有一句话解释为:

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

我找到了description offered here更有帮助。

因此,x: _*就像一个类型声明,告诉编译器将x视为重复参数 (又名可变长度参数列表 - vararg)。

关于scala - lift站点地图中冒号、下划线和星号的含义是什么(条目 :_*)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1124099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com