What is difference between composite keys and composite references? Purpose is not clear from docs, so I'd like to see some examples.
A composite key is a key derived from a set of keywords. It’s useful when you don’t need an explicit name for a key, you just want a key that’s derived from certain ancestors.
A composite reference is similar, but from the other direction. A composite reference looks for a key that’s derived from a set of other keys. So you can say, “Find me a key that has an ancestor ::a
and an ancestor ::b
”
To give you an example, a composite key [::a ::b]
means, “Create a new key that is derived from ::a
and ::b
“, while a composite ref #ig/ref [::a ::b]
means “Find me a key in the map that’s derived from ::a
and ::b
“.
Are these linked to multimethods in any way?
Only in the same way normal keys are.
So, in this example, does :example/web-1
derives from :adapter/jetty
? similar for :example/web-2
No, [:adapter/jetty :example/web-1]
derives from :adapter/jetty
.
But example/web-1
on its own does not.
I don't understand what implications does it have.
It's first time I saw a vector of keywords being derived from a keyword.
Behind the scenes, the vector is being replaced.
So say you had a map {[::a ::b] {:foo 1}}
Actually let’s make it a:
(def config {[::a ::b] {:foo 1}})
It looks for keys that are vectors, and replaces them with a randomly named keyword that derives from the elements in the vector.
(derive ::ab-1232 ::a)
(derive ::ab-1232 ::b)
(def config {::ab-1232 {:foo 1}})
Thanks a lot.
>while a composite ref #ig/ref [::a ::b]
means “Find me a key in the map that’s derived from ::a
and ::b
“.
Which map is looked for keys in this case?
The same map the ref is in.
The configuration map.
Ah so, it references already generated composite keys.
Yes, though they don’t have to be composite keys. You could create a key manually and they’d still work, e.g.
(derive ::ab ::a)
(derive ::ab ::b)
(def config {::ab 1, ::c (ig/ref [::a ::b])})
So that would still work, because ::ab
is derived from both ::a
and ::b
. We’ve just derived it explicitly.