On Wednesday, 11 December 2013 13:37:24 UTC, Philipp Meier wrote:
>
> Hi,
>
> Am Mittwoch, 11. Dezember 2013 14:27:01 UTC+1 schrieb Mikera:
>>
>> 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....)
>>
>
> Implementing a clojure protocol will give you fast dispatch on the first 
> argument's type. 
>

Very true... it's a tradeoff:
- protocols allow open extension (which doesn't appear to be needed here?)
- instance? checks are even faster (although protocol dispatch is still 
pretty fast)
- protocols have some other quirks (e.g. not allowing primitive arguments)

In the case we have here (a small fixed number of types, no open dispatch 
required) I think I would prefer instance? checks (especially given a nice 
macro to remove the boilerplate). 

-- 
-- 
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