2

I have java function like this

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

I'd like to make a partially apply function from this and specify some argument for Object... part. I am not sure if this is possible. I tried

val partiallyApply = createCollectionReader(_:Class[_ <: CollectionReader], _:TypeSystemDescription,
                                           "IncludeGoldStandardAnnotations", new Boolean("true"), 
                                           "EndIndex", new Integer("-1"), _:_*) // Doesn't work

and want it to be use as

val reader = partiallyApply(classOf[someReader], someType:TypeSystemDescription, 
"other", "configurationData", "beside", "those_four_that_already_applied_too"]

but this doesn't seems to work. Also, is this Object... has a technical name ?

EDIT: change code a little bit (my mistake .. I forgot to put val name in it) and add example of usage I want to.

EDIT2:I think my main question is that it is possible to do a partially apply function on vararg ?

EDIT3: Thanks to elbowich's suggestion. I come up with

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

work perfectly fine

hammar
  • 138,522
  • 17
  • 304
  • 385
Tg.
  • 5,608
  • 7
  • 39
  • 52
  • 2
    The `Object...` is called a [varargs](http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html#varargs). – Jeffrey Nov 26 '11 at 01:53
  • unclear - are you trying to create the method definition in scala or do you want to invoke the java method? the scala code you have offered has multiple flaws starting with a missing val name :-) let me know of what you are trying to do and i'll try to help! btw Object... is same as AnyRef* – aishwarya Nov 26 '11 at 04:15
  • thanks for pointing that out. I couldn't believe I forgot to put name in val... – Tg. Nov 26 '11 at 05:33
  • np :) is it possible to do a partially apply function on vararg - not directly, but varargs translate to arrays so you should be able to pass an array. – aishwarya Nov 26 '11 at 11:47

1 Answers1

2

I don't think you can partially apply varargs, I'd do it like this:

def method1(x: Int, cfg: String*) = cfg
def method2(x: Int, cfg: String*) = method1(x, Seq("preset", "cfg") ++ cfg:_*)
elbowich
  • 1,941
  • 1
  • 13
  • 12
  • 1
    That's wrong, it's perfectly possible to partially apply varargs, e.g. def tag(name: String)(attrs: Tuple2[String,String]*)(body: String*) = { etc... } – Alan Burlison Nov 26 '11 at 08:35
  • @AlanBurlison unfortunately that's not what I meant, but while we're at it: how one puts placeholder for `attrs` in your example? – elbowich Nov 26 '11 at 11:37
  • additionally, to my surprise, it's possible to do stuff like this: `def m(xs: Int*) = xs; val f = m(1, 2, _: Int)`, but looks like placeholder itself can't be a vararg – elbowich Nov 26 '11 at 11:50