Re: How do I use spec and deftype together?
Just in case someone stumbles over this post, I found a simple solution which works for me: (do (require '[clojure.spec.alpha :as s]) (import '(clojure.lang ISeq)) (defprotocol Foo (foo [this])) (defrecord Bar [bar] Foo (foo [this] (.bar this))) (deftype Baz [bar] Foo (foo [this] (.bar this)) ISeq (seq [this] `(~(.bar this (s/def ::bar number?) [(s/valid? (s/keys :req-un [::bar]) (->Bar 0)) (s/valid? (s/cat :bar ::bar) (->Baz 0))]) ;[true true] -- 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.
RE: How do I use spec and deftype together?
So is it best practice to implement map in the deftype to make it spec-able? Are there examples somewhere that show how to do this in a canonical way? If you want a `deftype` that is spec-able as a map, why not just use `defrecord`? Manually implementing the various map-related interfaces is a fair bit of boilerplate/work. Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN An Architect's View -- http://corfield.org/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood From: clojure@googlegroups.com on behalf of markus.ag...@gmail.com Sent: Wednesday, July 4, 2018 7:47:45 AM To: Clojure Subject: Re: How do I use spec and deftype together? So is it best practice to implement map in the deftype to make it spec-able? Are there examples somewhere that show how to do this in a canonical way? On Wednesday, July 4, 2018 at 12:51:29 PM UTC+2, Leon Grapenthin wrote: Spec doesn't check object fields. The defrecord case works because defrecord implements map. On Wednesday, July 4, 2018 at 12:47:26 PM UTC+2, markus...@gmail.com wrote: The same question was asked on https://clojurians-log.clojureverse.org/clojure-spec/2018-01-19 but I did not find an answer. An example, where defrecord works fine but deftype fails: (do (require '[clojure.spec.alpha :as s]) (defprotocol Foo (foo [this])) (defrecord Bar [bar] Foo (foo [this] (.bar this))) (deftype Baz [bar] Foo (foo [this] (.bar this))) (s/def ::bar number?) [(s/valid? (s/keys :req-un [::bar]) (->Bar 0)) (s/valid? (s/keys :req-un [::bar]) (->Baz 0))]) ;[true false] How do I have to change the last line so that it yields true as well? -- 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<mailto:clojure+unsubscr...@googlegroups.com>. 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.
Re: How do I use spec and deftype together?
So is it best practice to implement map in the deftype to make it spec-able? Are there examples somewhere that show how to do this in a canonical way? -- 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.
Re: How do I use spec and deftype together?
So is it best practice to implement map in the deftype to make it spec-able? Are there examples somewhere that show how to do this in a canonical way? On Wednesday, July 4, 2018 at 12:51:29 PM UTC+2, Leon Grapenthin wrote: > > Spec doesn't check object fields. The defrecord case works because > defrecord implements map. > > On Wednesday, July 4, 2018 at 12:47:26 PM UTC+2, markus...@gmail.com > wrote: >> >> The same question was asked on >> https://clojurians-log.clojureverse.org/clojure-spec/2018-01-19 >> but I did not find an answer. >> >> An example, where defrecord works fine but deftype fails: >> >> (do >> (require '[clojure.spec.alpha :as s]) >> (defprotocol Foo (foo [this])) >> (defrecord Bar [bar] Foo (foo [this] (.bar this))) >> (deftype Baz [bar] Foo (foo [this] (.bar this))) >> (s/def ::bar number?) >> [(s/valid? (s/keys :req-un [::bar]) (->Bar 0)) >>(s/valid? (s/keys :req-un [::bar]) (->Baz 0))]) ;[true false] >> >> >> How do I have to change the last line so that it yields true as well? >> > -- 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.
Re: How do I use spec and deftype together?
Spec doesn't check object fields. The defrecord case works because defrecord implements map. On Wednesday, July 4, 2018 at 12:47:26 PM UTC+2, markus...@gmail.com wrote: > > The same question was asked on > https://clojurians-log.clojureverse.org/clojure-spec/2018-01-19 > but I did not find an answer. > > An example, where defrecord works fine but deftype fails: > > (do > (require '[clojure.spec.alpha :as s]) > (defprotocol Foo (foo [this])) > (defrecord Bar [bar] Foo (foo [this] (.bar this))) > (deftype Baz [bar] Foo (foo [this] (.bar this))) > (s/def ::bar number?) > [(s/valid? (s/keys :req-un [::bar]) (->Bar 0)) >(s/valid? (s/keys :req-un [::bar]) (->Baz 0))]) ;[true false] > > > How do I have to change the last line so that it yields true as well? > -- 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.