Fork me on GitHub
#clojure-spec
<
2021-08-23
>
vemv04:08:12

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?)

reefersleep10:08:30

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

reefersleep10:08:59

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.

reefersleep10:08:52

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

vemv10:08:09

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)

reefersleep10:08:10

hm, yeah, I guess 🙂

reefersleep10:08:05

Looking forward to see how you fare 🙂

🙂 2
Lennart Buit11:08:11

Isn’t s/form close to what you are looking for?

👍 2
vemv11:08:44

I'm interested in newline/comment preservation as well

bortexz18:08:26

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)18:08:58

schemas are not exclusive

bortexz18:08:20

Great, thank you! I think it’s great to contextually ‘extend’ schema selections to keywords not originally on the schema

bortexz18:08:46

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

bortexz18:08:02

I see, that makes sense, thanks