I have the following record:
(defrecord Signal [samples ^double sample-rate ^double scaling-factor])
How can I specify samples
to be a double array?
I am using clojure 1.2.0
Edit:
@dreish I get the following output when I call (show Signal)
after the changes from levand:
[35] <init> (Object,double,double)
[36] <init> (Object,double,double,Object,Object)
[37] __extmap : Object
[38] __meta : Object
[39] sample_rate : double
[40] samples : Object
[41] scaling_factor : double
I know that hinting of non-primitive type is just used to avoid reflection. From http://clojure.org/datatypes
Thanks everybody!
Like this:
(defrecord Signal [^doubles samples ^double sample-rate ^double scaling-factor])
Rich Hickey added this a while back:
Added special type hints for primitive arrays - #^ints, #^floats, #^longs, #^doubles
See http://clojure.org/news for a discussion of how it works.
I don't have a Clojure environment with me to see if this is still the best way to do it. I assume the #^ syntax was updated to ^ along with all the other type hints in Clojure in 1.2
Edit: Another blog post on it: http://asymmetrical-view.com/2009/07/02/clojure-primitive-arrays.html
I agree with levand's answer on which type hint to use, but you might want to check whether defrecord
actually uses these type hints. On my installation (also 1.2.0), it does not.
user=> (defrecord Signal [^doubles samples ^double sample-rate ^double scaling-factor])
user.Signal
user=> (use '[clojure.contrib.repl-utils :only [show]])
nil
user=> (show Signal)
=== public final user.Signal ===
[stuff deleted]
[38] <init> (Object,Object,Object)
[39] __extmap : Object
[40] __meta : Object
[41] sample_rate : Object
[42] samples : Object
[43] scaling_factor : Object
[more stuff deleted]
As you can see, the constructor arguments (38) and the member variables (41-43) are still just Object
s. (Arrays are references anyway, but it will be nice to someday be able to store unboxed numbers in a record once that feature is implemented.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With