On Oct 19, 2016 7:23 AM, "Paul Gowder" <paul.gow...@gmail.com> wrote: > > Hi folks, > > I have a kinda basic question (I think?) that I should really know the answer to after writing Clojure for like 8 months, but don't. It's about requiring namespaces with protocol definitions in them but not the actual implementations of those protocols, and how it works. > > tl/dr: if a protocol is defined in namespace a and implemented (extended to the type at issue) in namespace b, can you call functions from it in namespace c where namespace c only requires namespace a, not namespace b? > > Backstory: I was poking around core.matrix recently in pursuit of restarting work on a months ago issue I promised to work on, and it looks like at least one namespace calls a function it shouldn't be able to call. > > So: clojure.core.matrix.impl.pprint requires only one namespace within core.matrix, clojure.core.matrix.protocols. (I'm going to use ... hereafter to stand for clojure.core.matrix.) ...protocols defines a protocol PSliceSeq2, which gives the name, docstring, etc. for a function called get-slice-seq. PSliceSeq2 is actually implemented for the generic object case in ...impl.defaults and for ISeq in ...impl.sequence. > > But ...impl.pprint calls ...protocols/get-slice-seq. And it seems to work. Even though I can't find either ...impl.defaults or ...impl.sequence anywhere in the dependency tree for ...impl.pprint. > > This is a puzzle to me. I mean, the ...impl.pprint namespace will typically be required from some other namespace, and presumably that higher-level namespace will require everything that one needs. But still, I hadn't thought that one would be able to call get-slice-seq in a function in ...impl.pprint without requiring a namespace that actually defines the function, as opposed to just defining the protocol. > > So can someone explain how this magic works? Is the compiler just way smarter than I thought it was, and capable of figuring this stuff out on its own? > Hi Paul,
can't give you specifics but (going from memory) I might be able to point you in the right direction. take a look at the initialization code of core.matrix. you have to feed it an implementation, and it dynamically arranges bindings. sorry I can't give you more detail but I suspect you'll find your answer somewhere in that vicinity. Gregg -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.