Hi,

Thank you for your help, but actually a factory function is not quite what 
I meant.

My problem is that when I call assoc for the record it will create a new 
record calling the class constructor directly, not the factory function. So 
even if I add additional checks to the factory function and make sure that 
is the only public function used to create records of that type, it's still 
possible to assoc/update/whatever the record and get around the checks. 
What I would like to override is the actual Java (or JavaScript) 
constructor used to initialize a new record. That way it would be called 
even when creating new records of that type from inside the record itself.

For reference about where the internal clones are created, see:

https://github.com/clojure/clojure/blob/ae7acfeecda1e70cdba96bfa189b451ec999de2e/src/clj/clojure/core_deftype.clj#L177
https://github.com/clojure/clojure/blob/ae7acfeecda1e70cdba96bfa189b451ec999de2e/src/clj/clojure/core_deftype.clj#L223
https://github.com/clojure/clojure/blob/ae7acfeecda1e70cdba96bfa189b451ec999de2e/src/clj/clojure/core_deftype.clj#L225
https://github.com/clojure/clojure/blob/ae7acfeecda1e70cdba96bfa189b451ec999de2e/src/clj/clojure/core_deftype.clj#L228

The best way to get the results I would like, seems to be to basically 
duplicate the whole defrecord code, which would feel very silly. I'm 
interested in knowing is there another way to make sure that a record 
always satisfies some rules (and is modified to satisfy those if it 
doesn't).


Juho

On Sunday, December 27, 2015 at 7:37:17 PM UTC+2, Magomimmo wrote:
>
> A custum constructor (aka factory function) is even considered idiomatic
>
>
> https://github.com/swannodette/lt-cljs-tutorial/blob/master/lt-cljs-tutorial.cljs#L1289
>
> HIH
>
> mimmo
>
>
> On 27 Dec 2015, at 17:22, juh...@gmail.com <javascript:> wrote:
>
> I'm wondering about the record usage where a record would have relations 
> between its fields. This would be best explained with the following example:
>
> 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 <javascript:>
> 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 <javascript:>
> 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 <javascript:>.
> 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 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