clojure-spec 2021-08-23

Currently, on spec alpha 2, you can select things that are not in the schema passed. Is this expected behaviour?

(ns user
  (:require [clojure.alpha.spec :as s]))

(s/def ::schema-field1 int?)
(s/def ::schema-field2 int?)
(s/def ::not-schema-field int?)

(s/def ::schema (s/schema [::field1 ::field2]))
(s/def ::selected (s/select ::schema [::schema-field1
                                      ::schema-field2
                                      ::not-schema-field]))

(s/valid? ::selected {::schema-field1 1
                      ::schema-field2 2
                      ::not-schema-field true}) => false

(s/valid? ::selected {::schema-field1 1
                      ::schema-field2 2
                      ::not-schema-field 3}) => true

Alex Miller (Clojure team) 2021-08-23T18:18:58.067700Z

schemas are not exclusive

Alex Miller (Clojure team) 2021-08-23T18:19:15.067900Z

so yes

Great, thank you! I think itโ€™s great to contextually โ€˜extendโ€™ schema selections to keywords not originally on the schema

Is there any reason to have a schema in select at all? This:

(s/select ::schema [::schema-field1
                    ::schema-field2
                    ::not-schema-field]))
and this:
(s/select [::schema-field1
           ::schema-field2
           ::not-schema-field]))
seem to express the same

Alex Miller (Clojure team) 2021-08-23T18:30:44.072900Z

gen

Alex Miller (Clojure team) 2021-08-23T18:30:52.073100Z

docs

Alex Miller (Clojure team) 2021-08-23T18:31:01.073300Z

closed schema checking

I see, that makes sense, thanks

Given an fdef , can I get its source code back? Like clojure.repl/source does with vanilla defs (my guess is that nope, but maybe someone wrote a thing?)

Maybe create a macro that uses a central registry to keep track of the source of each fdef, as per its fully evaluated name.

Not cool enough with macros to write the example, but itโ€™d be something like, swap!ing into your central atom, where you keep a map of fdef-symbol -> unevaluated source, and then of course evaluating your fdef normally afterwards.

This is just an off-hand idea, hope it makes sense

I think a minimalistic macro is a step in the right direction, thanks! Hadn't thought of that much In that case I wouldn't need an extra atom - I could simply let clojure.repl/source do its work e.g. (defmacro def-fdef ..) + (def-fdef the-defn the-spec) The macro would create a predictable symbol, like the-defn-fdef-source . Then I (source the-defn-fdef-source)

hm, yeah, I guess ๐Ÿ™‚

Looking forward to see how you fare ๐Ÿ™‚

๐Ÿ™‚ 1

Isnโ€™t s/form close to what you are looking for?

๐Ÿ‘ 1

I'm interested in newline/comment preservation as well