byte-array woes

341 views
Skip to first unread message

Karsten Schmidt

unread,
Mar 2, 2013, 12:22:51 PM3/2/13
to clo...@googlegroups.com
Hi,

even though I've using byte arrays many times, I'm still massively
confused each time I use them...

For example, why does this fail?

(byte-array [1 2 3 4])
ClassCastException java.lang.Long cannot be cast to java.lang.Byte
clojure.lang.Numbers.byte_array (Numbers.java:1223)

I understand ints are longs by default, but all of the values are in
byte-range and should not cause a casting error. Also creating an
int-array with the same values works just fine (and casts values
correctly):

(int-array [1 2 3 4])

Casting each value specifically to byte first works on the other hand:
(byte-array (map byte [1 2 3]))

Is that a bug or can someone please explain why bytes seem to require
special treatment in Clojure?

Thanks! K.
--
Karsten Schmidt
http://postspectacular.com | http://toxiclibs.org | http://toxi.co.uk

Marko Topolnik

unread,
Mar 2, 2013, 1:43:18 PM3/2/13
to clo...@googlegroups.com
On Saturday, March 2, 2013 6:22:51 PM UTC+1, Karsten Schmidt wrote:

Is that a bug or can someone please explain why bytes seem to require
special treatment in Clojure?

Calling it a bug wouldn't be entirely fair since it's a missing feature. I'd say this is filable as an enhancement request.

-Marko 

Karsten Schmidt

unread,
Mar 2, 2013, 5:49:52 PM3/2/13
to clo...@googlegroups.com
Thanks, Marko! I'd count this as a sort of bug though... at least in
terms of consistency, since it breaks expected behaviour as
established by other common array c'tor functions:

(int-array [1 2 3]) => ok
(long-array [1 2 3]) => ok
(float-array [1 2 3]) => ok
(double-array [1 2 3]) => ok
(byte-array [1 2 3]) => fail
(short-array [1 2 3]) => fail
(char-array [1 2 3]) => fail, but bearable since you'd use shorts
anyway if you specify chars as numbers

I had a look at clojure.lang.Numbers.java and found a fix for both
bytes & shorts:

Whereas all the working array c'tors functions are using this pattern:

array[i] = ((Number)s.first()).intValue();

... the byte-array & short-array versions attempt to cast the sequence
items directly, i.e.

array[i] = (Byte)s.first();

Changing this to the first pattern (via Number) fixes the issue and
I'll submit a patch for this...

Hth! K.
> --
> --
> 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
> ---
> 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+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Frank Siebenlist

unread,
Mar 2, 2013, 7:01:40 PM3/2/13
to clo...@googlegroups.com, Frank Siebenlist
I just ran into that issue while I was constructing byte-arrays for secure-hash test cases.

Ended-up using (byte-array (vector-of :byte 1 2 3 4)) to avoid writing the (byte-array [(byte 1)(byte 2)(byte 3)(byte 4)]).

Transparently adding valid byte-number values to a byte-array makes sense and would be helpful.

Thanks, FrankS.

Karsten Schmidt

unread,
Mar 4, 2013, 9:30:20 AM3/4/13
to clo...@googlegroups.com, Frank Siebenlist
Hi Frank, I've added a patch to CLJ-766 with a potential fix. Also,
Andy F. kindly pointed out that interested parties should vote on that
issue to help increase its visibility...

Add your 2 cents here (provided you've a CA):
http://dev.clojure.org/jira/browse/CLJ-766

Best, K.

Frank Siebenlist

unread,
Mar 4, 2013, 2:02:27 PM3/4/13
to Karsten Schmidt, clo...@googlegroups.com
I gave it my vote - thanks for trying to fix this issue - FrankS.

FrankS (from mobile)
Reply all
Reply to author
Forward
0 new messages