user=> (defrecord Time [minutes seconds])
user.Time
user=> (->Time 0 59)
#user.Time{:minutes 0, :seconds 59}
user=> (update (->Time 0 59) :seconds inc)
#user.Time{:minutes 0, :seconds 60}
It's not very nice since 60 seconds is not really a valid value. If it would be possible to define so that
user=> (Time. 0 60)
#user.Time{:minutes 1, :seconds 0}
then if I'm reading emit-defrecord correctly, assoc and therefore update etc. would work as expected here. Would this be semantically against principles of record or could some kind of structure to modify the fields while creating a new instance of a record be considered as part of Clojure?
Juho
--
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/d/optout.
In clojure, doing this will give you a compile-time error, meaning you have to reimplement all the functionality of a defrecord using deftype, even for protocols you do not actually want to override.
The Potemkin library has some macros to make this kind of thing less tedious, I recommend taking a look: https://github.com/ztellman/potemkin
Hmm, I have to try out that extend-type trick to silence the warnings, although using defrecord for clojurescript and not for clojure would feel awkward as well.
> In clojure, doing this will give you a compile-time error, meaning you have to reimplement all the functionality of a defrecord using deftype, even for protocols you do not actually want to override.
>
> The Potemkin library has some macros to make this kind of thing less tedious, I recommend taking a look: https://github.com/ztellman/potemkin
Oh, I'd had a look at Potemkin before in other context, but had forgotten it also has def-map-type which helps. I'll go look there for inspiration. In any case it looks like custom deftype and implementing map functionality on top of that would be the way to go.
Thanks a lot for your help in clearing out things. Would still hope for the records to be a bit more extensible, having a typed map that can implement protocols and avoids lookups for fields is nice, but doesn't really bring that much benefits over plain maps and functions IMHO.