Using `^:const` can lead to some funky behaviour, like the one you're seeing. AFAIK The only official documentation about it can be found in the clojure 1.3 changelog, https://github.com/clojure/clojure/blob/master/changes.md#215-const-defs where it is described as "^:const lets you name primitive values with speedier reference.".
There are other values that I'd say are currently "safe" to be used with `^:const` other than primitive values, such as strings and keywords, but I wouldn't use it for anything else, since it was not designed to work with arbitrary values but just to speed up math operations. Nicola > On 19 Apr 2016, at 15:43, Michael Stalker <[email protected]> wrote: > > Adding ^:const to a var seems to change the data type sometimes: > > user> (def ^:const time1 (java.sql.Timestamp. 100000000) > #'user/time1 > user> (def time2 (java.sql.Timestamp. 100000000) > #'user/time2 > user> (type time1) > java.util.Date > user> (type time2) > java.sql.Timestamp > > The upshot is that while (= time1 time2) yields true, converting these two > vars to strings yields different results: > > user> (str time1) > "Thu Jan 01 22:46:40 EST 1970" > user> (str time2) > "1970-01-01 22:46:40.0" > > Daniel Gregoire pointed out that this seems be because of how the compiler > quotes constant vars. He showed met that eval-ing two different methods of > quoting yields different types: > > user> (type (eval (quote (java.sql.Timestamp. 100000000)))) > java.sql.Timestamp > user> (type (eval (list 'quote (java.sql.Timestamp. 100000000)))) > java.util.Date > > Chris Bui noted that rebinding the data readers, and then evaluating the > second form above, yields the same data type as the first form: > > user> (prn default-data-readers) > {inst #'clojure.instant/read-instant-date, uuid > #'clojure.uuid/default-uuid-reader} > nil > user> (binding [*data-readers* {'inst > #'clojure.instant/read-instant-timestamp}] > (type (eval (list 'quote (java.sql.Timestamp. 100000000))))) > java.sql.Timestamp > > Here's a gist he made. > > I didn't expect ^:const to change the var's data type, and there wasn't any > clear documentation on this behavior that I could find. Is this a bug that I > should file in JIRA, or is ^:const supposed to work like this? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > [email protected] > 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 [email protected]. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: Message signed with OpenPGP using GPGMail
