Re: s/valid returns false, but s/explain prints "Success!"

2019-09-26 Thread Markus Agwin
https://www.clojure.org/guides/spec are the docs I mentioned (search for 
first occurrence of :kind)
Thank you very much! I have foolishly only checked for s/explains's 
"Success!" string, having omitted the s/valid? check to save runtime.

On Thursday, September 26, 2019 at 2:54:03 PM UTC+2, Alex Miller wrote:
>
>
>
> On Thursday, September 26, 2019 at 7:41:20 AM UTC-5, Markus Agwin wrote:
>>
>> Consider the following cloure.spec-alpha2 example:
>>   
>> (def v [0])
>> (s/def ::thevec vector?)
>> (s/def ::data (s/coll-of number? :kind ::thevec))
>>
>
> :kind is expected to be a predicate, not a spec. The doc string (in 
> s/every) says ":kind - a pred that the collection type must satisfy, e.g. 
> vector?"
>
> so this should be:
>
> (s/def ::data (s/coll-of number? :kind vector?))
>
> With the prior, you're getting the keyword ::thevec as the predicate - 
> that will look ::thevec up in whatever you pass it when used as a predicate.
>  
>
>> (s/valid? ::data v) ;;=> returns false, my expectation is that it should 
>> return true
>>
>
> This matches above interpretation - (::thevec [0]) yields nil
>  
>
>> (s/explain ::data v) ;;=> prints "Success!", which inconsistent to 
>> s/valid?
>>
>
> Agreed that's confusing. I think it's because the keyword as predicate 
> returns nil instead of false but there is probably something to clean up 
> here.
>  
>
>>
>> (s/def ::data2 (s/coll-of number? :kind vector?))
>> (s/valid? ::data2 v) ;;=> returns true, as expected
>>
>> I expect (s/valid? ::data v) to return true, but it returns false. 
>> Moreover, s/explain prints "Success!" which is inconsistent to s/valid? for 
>> this example.
>> If I replace :kind ::thevec by :kind vector?, everything is fine. Why is 
>> that (the spec documentation says: ":kind - a predicate or spec").
>>
>
> The docs were wrong at one point in time, but this was fixed a while ago, 
> only preds are supported here.  Where are you seeing the old docs? 
>
>
> https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/every
>  
> doesn't have that and you won't see it in docstrings of current spec or 
> spec 2 versions
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/0827ace3-4c91-47c4-aec9-5b70c92cc6a3%40googlegroups.com.


s/valid returns false, but s/explain prints "Success!"

2019-09-26 Thread Markus Agwin
Consider the following cloure.spec-alpha2 example:
  
(def v [0])
(s/def ::thevec vector?)
(s/def ::data (s/coll-of number? :kind ::thevec))

(s/valid? ::data v) ;;=> returns false, my expectation is that it should 
return true
(s/explain ::data v) ;;=> prints "Success!", which inconsistent to s/valid?

(s/def ::data2 (s/coll-of number? :kind vector?))
(s/valid? ::data2 v) ;;=> returns true, as expected

I expect (s/valid? ::data v) to return true, but it returns false. 
Moreover, s/explain prints "Success!" which is inconsistent to s/valid? for 
this example.
If I replace :kind ::thevec by :kind vector?, everything is fine. Why is 
that (the spec documentation says: ":kind - a predicate or spec").

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/70b194ee-703f-48fa-9b1a-915f5dbf90d6%40googlegroups.com.


Re: How do I use spec and deftype together?

2018-07-05 Thread markus . agwin
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?

2018-07-04 Thread markus . agwin
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?

2018-07-04 Thread markus . agwin
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.


How do I use spec and deftype together?

2018-07-04 Thread markus . agwin
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.