从java函数制作部分应用函数

时间:2011-11-26 01:39:14

标签: java scala partial-application

我有像这样的java函数

public static CollectionReader createCollectionReader(
        Class<? extends CollectionReader> readerClass, TypeSystemDescription typeSystem,
        Object... configurationData) 

我想从中创建一个部分应用函数,并为Object ... part指定一些参数。我不确定这是否可行。我试过了

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:感谢elbowich的建议。我想出了

def createCollectionReaderReadAll(cls: Class[_ <: CollectionReader], ts: TypeSystemDescription, cfg: AnyRef*) =
  createCollectionReader(cls, ts,
    Seq("IncludeGoldStandardAnnotations", new Boolean("true"), "EndIndex", new Integer("-1")) ++ cfg: _*)

工作得很好

1 个答案:

答案 0 :(得分:2)

我认为你不能部分应用varargs,我会这样做:

def method1(x: Int, cfg: String*) = cfg
def method2(x: Int, cfg: String*) = method1(x, Seq("preset", "cfg") ++ cfg:_*)
相关问题