Interop question concerning optional args

215 views
Skip to first unread message

JonathanBelolo

unread,
Sep 27, 2010, 4:16:11 PM9/27/10
to Clojure
While toying with the Sesame2.3 library, I've come across the
following behavior for the first time.

This is taken from the api doc for
org.openrdf.repository.base.RepositoryConnectionBase:

add(Resource subject, URI predicate, Value object, Resource...
contexts)
Adds a statement with the specified subject, predicate and
object to this repository, optionally to one or more named contexts.

But apparently, Clojure seems to think the optional args are
mandatory...

(.add con alice RDF/TYPE person)

No matching method found: add for class
org.openrdf.repository.sail.SailRepositoryConnection
[Thrown class java.lang.IllegalArgumentException]

So I run

(grep #".add" (.getMethods (.getClass con)))

#<Method public void
org.openrdf.repository.base.RepositoryConnectionBase.add(org.openrdf.model.Resource,org.openrdf.model.URI,org.openrdf.model.Value,org.openrdf.model.Resource[])
throws org.openrdf.repository.RepositoryException>)

Finally the following works...

(.add con alice RDF/TYPE person (make-array Resource 1))
nil

Is this behavior normal? Are optional args mandatory when called with
interop?

Thanks for your help :)

Jonathan

ataggart

unread,
Sep 27, 2010, 8:20:04 PM9/27/10
to Clojure
The vararg at the end of the method is just syntactic sugar for an
array, so the "add" method actually takes 4 args, the last being a
Resource array. The java compiler just replaces "missing" varargs
with an empty array.

My guess is that the reflection mechanisms in the compiler just look
at type/arity. The Method object has a isVarArg() boolean, so that
could be used to allow omitting varargs altogether. That would need
to be an enhancement to the clojure compiler, so I opened a ticket:

https://www.assembla.com/spaces/clojure/tickets/440-java-method-calls-cannot-omit-varargs


On Sep 27, 1:16 pm, JonathanBelolo <jonat...@scorpiomusic.fr> wrote:
> While toying with the Sesame2.3 library, I've come across the
> following behavior for the first time.
>
> This is taken from the api doc for
> org.openrdf.repository.base.RepositoryConnectionBase:
>
> add(Resource subject, URI predicate, Value object, Resource...
> contexts)
>           Adds a statement with the specified subject, predicate and
> object to this repository, optionally to one or more named contexts.
>
> But apparently, Clojure seems to think the optional args are
> mandatory...
>
> (.add con alice RDF/TYPE person)
>
> No matching method found: add for class
> org.openrdf.repository.sail.SailRepositoryConnection
>   [Thrown class java.lang.IllegalArgumentException]
>
> So I run
>
> (grep #".add" (.getMethods (.getClass con)))
>
> #<Method public void
> org.openrdf.repository.base.RepositoryConnectionBase.add(org.openrdf.model. Resource,org.openrdf.model.URI,org.openrdf.model.Value,org.openrdf.model.Re source[])

Dave Kincaid

unread,
Dec 10, 2012, 11:54:53 PM12/10/12
to clo...@googlegroups.com, alex.t...@gmail.com
I just came across this same problem while trying to use Java 7's java.nio.file.Files.createTempDirectory() (http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempDirectory(java.lang.String, java.nio.file.attribute.FileAttribute...))

Clojure won't let me just do (java.nio.file.Files/createTempDirectory "mydir")

It wants the FileAttribute argument. Can anyone help me get past this? I'm stuck since I really can't figure out how to create a FileAttribute. Am I better off just using Apache commons or something like that?

Andy Fingerhut

unread,
Dec 11, 2012, 1:44:34 AM12/11/12
to clo...@googlegroups.com
You can pass in a length 0 array of java.nio.file.attribute.FileAttribute's like so:

(java.nio.file.Files/createTempDirectory "mytempname" (make-array java.nio.file.attribute.FileAttribute 0))

Andy


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Vladimir Tsichevski

unread,
Dec 11, 2012, 7:30:01 AM12/11/12
to clo...@googlegroups.com
I found this (mis)feature quite annoying too. I think, we all shall ask the language authors to fix it.

Andy Fingerhut

unread,
Dec 11, 2012, 1:06:30 PM12/11/12
to clo...@googlegroups.com
There is a ticket filed for it, in case some contributor wants to update the patches for it.  It got intertwined with CLJ-445's patch, which hasn't been updated in a while, so you might want to start fresh rather than untangle the history there.

http://dev.clojure.org/jira/browse/CLJ-440

Andy
Reply all
Reply to author
Forward
0 new messages