charAt returns a char, not a primitive int, which is why you are getting 
the "ClassCastException java.lang.Character cannot be cast to 
java.lang.Number" exception.

The long coercion function will inline to `(. clojure.lang.RT (longCast ~x)), 
and since that that is a static method which lacks a primitive signature 
for char, that explains your first exception.

Clojure primitive function support is limited to longs and doubles, which 
is fairly reasonable given that the places where this might actually matter 
(in terms of performance) would be tight numerical loops where the cost of 
repeated function invocation would degrade performance to an unreasonable 
degree. I don't think that's the case with this function, but you can 
decide based on your use case since only you know your program best. 

In any event, to solve the reflection warning here you should add a 
non-primitive type hint to the character returned by charAt so that it will 
match the Object method of longCast. ^Character will suffice. 

On Friday, October 3, 2014 11:41:48 AM UTC-4, Fluid Dynamics wrote:
>
> OK, can someone tell me what the hell is going on here?
>
> (defn alphanumeric?
>   "Given a character, returns true iff the character is alphanumeric.
>    Accented letters and other things that pass Character/isAlphabetic are
>    all counted as alphanumeric."
>   [^long c]
>   (or (Character/isDigit c) (Character/isAlphabetic c)))
>
> ...
>
> (let [^String s "foo"]
>   (alphanumeric? (long (.charAt s i))))
>
> Reflection warning, blah blah blah - call to longCast can't be resolved.
>
>
> How do I get rid of reflection here? If I drop the (long ... ) around the 
> charAt call, it doesn't even compile:
>
> ClassCastException java.lang.Character cannot be cast to java.lang.Number
>
> which is even weirder, as it suggests that the return value from .charAt 
> is being boxed, which it shouldn't be if it's going to go directly into 
> interop or a primitive-taking function.
>
> None of the apparent fixes strike me as very attractive here:
>
> 1. Get rid of alphanumeric? and just put the or expression everywhere.
>
> So much for code readability, DRY, and the vaunted code size efficiency of 
> Lisps.
>
> 2. Turn the function into a macro, to avoid the repetition in 1.
>
> But now it won't work in (every? alphanumeric? some-string) and wrapping 
> it as #(alphanumeric? %) bloats things, though not as badly as before.
>
> 3. definline seems to offer the best of both worlds
>
> But still bloats generated bytecode...
>
>
> Even supposing a nicer solution presented itself, the implication that 
> .charAt's return is being boxed is troubling. Is there no way (aside from 
> turning these parts of the code into a Java class full of utility methods) 
> to use char values without boxing, short of (or even) directly passing to 
> interop?
>
> Also: is there any way to make something as elegant as (every? 
> alphanumeric? s) that's efficient? Something involving the new reducers, 
> perhaps? It smells like a species of reduction to me...
>
> Also: How does one profile in Counterclockwise? Googling for 
> counterclockwise profile clojure didn't bear fruit, and googling for 
> eclipse profile java turned up that there's apparently supposed to be a 
> "Profiling and Logging perspective" which is missing, at least in the Open 
> Perspective dialog in the standalone CCW install 
> (Counterclockwise-0.28.1.STABLE001-win32.win32.x86_64.zip, current as of a 
> few days ago).
>
>

-- 
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/d/optout.

Reply via email to