2

我有这样的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: _*)

工作得很好

4

1 回答 1

2

我认为您不能部分应用可变参数,我会这样做:

def method1(x: Int, cfg: String*) = cfg
def method2(x: Int, cfg: String*) = method1(x, Seq("preset", "cfg") ++ cfg:_*)
于 2011-11-26T06:58:39.633 回答