Fork me on GitHub
#clojure-spec
<
2020-05-15
>
Amir Eldor18:05:45

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?

Alex Miller (Clojure team)19:05:36

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

Alex Miller (Clojure team)19:05:15

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 Eldor19:05:09

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

Alex Miller (Clojure team)19:05:36

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

Alex Miller (Clojure team)19:05:52

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

Alex Miller (Clojure team)19:05:55

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 Eldor19:05:12

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!

Amir Eldor19:05:20

I see that when I do something like

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

Amir Eldor19:05:46

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

Amir Eldor19:05:51

Which is good for me

Amir Eldor19:05:38

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

Alex Miller (Clojure team)19:05:49

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 Eldor19:05:40

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?

Alex Miller (Clojure team)19:05:58

:req-un means "required unqualified key"

Alex Miller (Clojure team)19:05:28

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

Alex Miller (Clojure team)19:05:40

conforming just follow that

Amir Eldor19:05:16

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

Amir Eldor19:05:53

Interesting and cool.

Amir Eldor19:05:11

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

Amir Eldor19:05:28

Oh nevermind.