【发布时间】:2011-11-26 01:39:13
【问题描述】:
我有这样的java函数
public static CollectionReader createCollectionReader(
Class<? extends CollectionReader> readerClass, TypeSystemDescription typeSystem,
Object... configurationData)
我想从中创建一个部分应用函数,并为 Object... 部分指定一些参数。我不确定这是否可能。我试过了
val partiallyApply = createCollectionReader(_:Class[_ <: CollectionReader], _:TypeSystemDescription,
"IncludeGoldStandardAnnotations", new Boolean("true"),
"EndIndex", new Integer("-1"), _:_*) // Doesn't work
并希望它被用作
val reader = partiallyApply(classOf[someReader], someType:TypeSystemDescription,
"other", "configurationData", "beside", "those_four_that_already_applied_too"]
但这似乎不起作用。另外,这个对象……有技术名称吗?
编辑:稍微更改代码(我的错误..我忘记在其中输入 val 名称)并添加我想要的用法示例。
EDIT2:我认为我的主要问题是可以对 vararg 执行部分应用功能吗?
EDIT3:感谢肘部的建议。我想出了
def createCollectionReaderReadAll(cls: Class[_ <: CollectionReader], ts: TypeSystemDescription, cfg: AnyRef*) =
createCollectionReader(cls, ts,
Seq("IncludeGoldStandardAnnotations", new Boolean("true"), "EndIndex", new Integer("-1")) ++ cfg: _*)
工作得很好
【问题讨论】:
-
Object...被称为varargs。 -
unclear - 您是要在 scala 中创建方法定义还是要调用 java 方法?您提供的 scala 代码有多个缺陷,以缺少 val 名称开头 :-) 让我知道您要做什么,我会尽力提供帮助!顺便说一句 Object... 与 AnyRef* 相同
-
感谢您指出这一点。我简直不敢相信我忘了把名字放在 val...
-
np :) 是否可以对 vararg 执行部分应用函数 - 不是直接,但 varargs 转换为数组,因此您应该能够传递数组。
标签: java scala partial-application