clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
Amir Eldor 2020-05-15T18:54:45.412800Z

So it's not intended, for example, to use things like ::mygameobject-id on other namespaces of specific 'gameobject' each? Among the things I'm playing around with is having a ::gameobject-id defined in some-ns.game and use that in some-ns.game.dog and some-ns.game.bat, each of which has an :id which is a gameobject-id. Am I doing things in a non-Clojure way?

alexmiller 2020-05-15T19:06:36.413200Z

:: means to auto-resolve the keyword in the context of the current namespace

alexmiller 2020-05-15T19:07:15.414Z

so if you're using ::id in different namespaces, you'll get different keywords specific to each namespace, and there is no conflicting name

Amir Eldor 2020-05-15T19:08:09.414700Z

and if I want to use the same spec definition for each? just use a common function?

alexmiller 2020-05-15T19:08:36.415300Z

you can (s/def ::id ::common/gameobject-id) in each namespace

alexmiller 2020-05-15T19:08:52.415700Z

so have one common spec and then make namespaced specs that refer to it

alexmiller 2020-05-15T19:10:55.417400Z

removing the potentially confusing auto-resolving keywords, something like:

(s/def :common/gameobject-id string?)
(s/def :some-ns.game.dog/id :common/gameobject-id)
(s/def :some-ns.game.bit/id :common/gameobject-id)

Amir Eldor 2020-05-15T19:12:12.418300Z

I was trying to do just that and it didn't work, and not it works. Great. Maybe I had a funky repl state before :man-shrugging: Thanks!

alexmiller 2020-05-15T19:12:24.418500Z

good!

Amir Eldor 2020-05-15T19:14:20.419300Z

I see that when I do something like

(s/def ::thingy (s/keys :req-un [:common/gameobject-id]))

Amir Eldor 2020-05-15T19:14:46.419900Z

Then the thingy gets an :id property, and not { :common/gameobject-id "whatever" }

Amir Eldor 2020-05-15T19:14:51.420100Z

Which is good for me

Amir Eldor 2020-05-15T19:15:38.421200Z

(or maybe :gameobject-id, I am mixing up some namings between this chat and my actual code)

alexmiller 2020-05-15T19:16:49.422300Z

it doesn't "get" a :gameobject-id property, it describes a map expected to have a :gameobject-id key which matches the :common/gameobject-id spec

Amir Eldor 2020-05-15T19:21:40.424100Z

I meant that when I conform something to ::thingy, the keyword in the conformed map is { :gameobject-id "something" } and not { :common/gameobject-id "something" }. I guess it omits the namespace from the keyword?

alexmiller 2020-05-15T19:21:58.424400Z

:req-un means "required unqualified key"

alexmiller 2020-05-15T19:22:03.424600Z

(as opposed to :req)

alexmiller 2020-05-15T19:22:28.425100Z

so more importantly it's validating the incoming map expecting an unqualified key

alexmiller 2020-05-15T19:22:40.425500Z

conforming just follow that

Amir Eldor 2020-05-15T19:23:16.426200Z

I seeeee I made a thingy2 now with :req and it wants the fully qualified key

Amir Eldor 2020-05-15T19:23:53.426600Z

Interesting and cool.

Amir Eldor 2020-05-15T19:40:11.427100Z

Do I have to require common for using :common/gameobject-id or something?

Amir Eldor 2020-05-15T19:43:28.427800Z

Oh nevermind.

alexmiller 2020-05-15T19:52:04.428Z

no