You can't really avoid the instance checks: the underlying mechanic is that 
the JVM needs to figure out somehow at runtime which overloaded method 
version to call, since all it knows at compile time is that it has an 
arbitrary Object. 

instance? is the simplest way to do this. There are some fancier ways (e.g. 
using reflection or invokedynamic) but your case doesn't have very complex 
dispatch requirements so instance? checks are almost certainly the best 
option.

The good news is that instance? checks are extremely fast on the JVM so 
performance will still be great (certainly far better than if you used any 
kind of reflection....)


On Tuesday, 10 December 2013 10:25:48 UTC, Phillip Lord wrote:
>
>
>
> I want to type hint an overloaded Java method. I used to have a function 
> like so 
>
> (defn iri [name] 
>  (IRI/create name)) 
>
> Where IRI.create is one of 
>
> IRI.create(String) 
> IRI.create(URL) 
> IRI.create(File) 
>
> Type hinting the return value of this is straight-forward, but the 
> parameter is one of three types. The only way I seem to be able to get 
> this to work is to do lots of instance? checks... 
>
>
> (defn ^IRI iri 
>   [name] 
>   (cond 
>    (instance? String name) 
>    (IRI/create ^String name) 
>    (instance? java.net.URL name) 
>    (IRI/create ^java.net.URL name) 
>    (instance? java.io.File name) 
>    (IRI/create ^java.io.File name))) 
>
> which is a lot longer and, well, just not very nice. I could make this 
> neater with a macro -- something like... 
>
> (with-types [String, URL, File] 
>    (IRI/create name)) 
>
> which would expand into the cond form above. But the instance? checks 
> seem not ideal. Is there a better solution? 
>
> Phil 
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to