Fork me on GitHub
#clojure-spec
<
2016-11-08
>
drewr16:11:07

@norton it's tricky... you have to make sure there's an fdef for the thing you're :replace-ing

norton16:11:55

@drewr Thank you for the feedback. I posted this question (with more details) to the Google clojure mailing list with an example that is using :replace. Alex Miller responded and corrected my example. It works as expected!

mpenet19:11:04

s/keys question, I need to express key ::foo is optional, but if it's there ::bar key should be too, since there's no or/and in opt it's a bit tricky I think.

mpenet19:11:04

I guess i ll split that in multiple s/keys

zane19:11:08

I don't think you can do all — yes.

zane19:11:22

That's what I'd do. ☺️

mpenet19:11:54

not sure why and/or is not supported in s/keys :opt, it would be useful

Alex Miller (Clojure team)19:11:48

Why not req (or foo (and foo bar))

zane19:11:57

I'd probably replace the #(or … with another (s/keys … so you get enforcement of the values for :foo and :bar.

mpenet19:11:10

@alexmiller because foo is required like this

mpenet19:11:40

it's should be optional, but if present there should be :bar too

mpenet19:11:01

@zane yep + s/merge support etc etc

Alex Miller (Clojure team)19:11:00

I don't think merge helps at all here

Alex Miller (Clojure team)19:11:19

But you could and a predicate that adds this constraint

mpenet19:11:25

it doesn't, I was mentioning it in the context of a solution with keys vs s/and + pred with contains?

Alex Miller (Clojure team)19:11:53

Or or 2 keys specs - one with the pair, one without

mpenet19:11:15

that's what I am going to end up using, but I wish it was possible to use or/and in opt

Alex Miller (Clojure team)19:11:17

Although that may not be very useful

Alex Miller (Clojure team)19:11:38

Opt is all optional - it doesn't make sense to have and/or there

mpenet19:11:37

well it'd make this example way nicer at least, there are other situations like this where it would make sense

Alex Miller (Clojure team)19:11:46

Note that this is or, not xor

mpenet19:11:37

to take the other example mentioned earlier: an option map that could have credentials (optionally), but if ::user is there there must be ::password too.

bbloom22:11:39

user=> (s/conform :clojure.core.specs/defn-args '(foo [{x :y}])) {:bs [:arity-1 {:args {:args [[:map {x :y}]]}}], :name foo}

bbloom22:11:45

user=> (s/conform :clojure.core.specs/defn-args '(foo [{x y}])) {:bs [:arity-1 {:args {:args [[:seq {:elems [[:seq {:elems [[:sym x] [:sym y]]}]]}]]}}], :name foo}

bbloom22:11:53

@alexmiller ^ is that a bug?

bbloom22:11:32

sorry, should have elaborated

bbloom22:11:55

the second evaluation returns {x y} with a :seq node instead of a :map node

bbloom22:11:38

glad i’m not the only one who couldn’t instantly puzzle out the destructuring specs 😉

Alex Miller (Clojure team)22:11:41

Not at a computer so hard for me to look at stuff

bbloom22:11:00

no worries - i’ll look at it a bit more and maybe file a bug - thanks

Alex Miller (Clojure team)22:11:34

I guess the question is whether regex specs like cat should or do work on maps

zane22:11:03

What's the recommended way to handle specs that are dependent on runtime values?

Alex Miller (Clojure team)22:11:28

If so, then seq-binding-form will happen to be satisfied by the map

Alex Miller (Clojure team)22:11:11

Or dynamically generate a spec

bbloom23:11:32

ok - i’m reasonably sure that is a bug & that the fix is a simple s/spec - making a patch

bbloom23:11:59

the spec splicing behavior is simultaneously useful and annoyingly subtle 🙂

bbloom23:11:30

decided to add a s/and vector? instead, since that seemed more reliable & more accurately spec'd (i think) - if not, happy to tweak