Giter Site home page Giter Site logo

Comments (13)

weavejester avatar weavejester commented on May 18, 2024

Composite keys are just keys that inherit from all of their keywords. If we wanted some way of combining specs, then we can't use multimethods, as inheritance doesn't combine.

I've actually written a library for combined inheritance, but whether this is the best way of handling things for Integrant I'm not certain, as it introduces a new dependency and new syntax.

from integrant.

weavejester avatar weavejester commented on May 18, 2024

Your example doesn't appear to match up to the rest of the issue. If you have a configuration:

{[:group/a :adapter/jetty] {:port 8080, :handler #ig/ref [:group/a :handler/greet]}
 [:group/a :handler/greet] {:name "Alice"}
 [:group/b :adapter/jetty] {:port 8081, :handler #ig/ref [:group/b :handler/greet]}
 [:group/b :handler/greet] {:name "Bob"}}

And specs:

(defmethod ig/pre-init-spec :adapter/jetty [_]
  (s/keys :req-un [::port ::handler]))

(defmethod ig/pre-init-spec :handler/greet [_]
  (s/keys :req-un [::name]))

Then it should work fine; I just tried it. It's only when you have multiple specs for the same keyword that the inheritance is forced to pick one.

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

@weavejester I'm not sure I understand because the two defmethods you quoted above never get invoked for that config. pre-init-spec dispatches on the four composite keys.

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

Composite keys are just keys that inherit from all of their keywords. If we wanted some way of combining specs, then we can't use multimethods, as inheritance doesn't combine.

I think I understand that constraint better now. Even my cursory glance at the problem already hinted at some sort of hierarchy-driven spec composition mechanism, which seems like it will inevitably lead to some tradeoffs.

But while this behavior is self-explanatory when looking at the source code, I think it should be mentioned in the README and/or the pre-init-spec docstring.

On the other hand I'm now wondering what kind of hooks could be provided so that if I have N composite keys, that should all somehow use the same spec from one of their components, I could define a single method for that hook rather the N boilerplate methods.

from integrant.

weavejester avatar weavejester commented on May 18, 2024

@weavejester I'm not sure I understand because the two defmethods you quoted above never get invoked for that config. pre-init-spec dispatches on the four composite keys.

Ah, you're right. There's a bug; pre-init-spec doesn't normalize the composite keys first.

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

@weavejester I actually noticed that discrepancy but ig/normalize-key returns something like :integrant.composite/group.a+handler.greet_35275 which seemed like something that would be used for an internal dispatch mechanism.

Could you describe what you think the behavior should be from the perspective of a user trying to write pre-init-spec methods on a composite key?

from integrant.

weavejester avatar weavejester commented on May 18, 2024

The behaviour should be the same as all the other multimethods, in that composite keys should be treated as keywords that derive from all the keywords contained in the vector.

In other words, this:

[:group/a :handler/greet]

Should have the same semantic meaning as:

(derive :temp/a :group/a)
(derive :temp/a :handler/greet)
:temp/a

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

I'm sorry it's still not clear to me what method(s) would get dispatched for that composite key, and if there are more than one, how would the different specs get combined?

from integrant.

weavejester avatar weavejester commented on May 18, 2024

It's the same rules as normal multimethods. The most specific key is chosen, and ties are broken by prefer-method.

So in the above case we have a key that's derived from :group/a and :handler/greet. Clojure will look for a multimethod that handles either of those. If there is a method for both, it uses prefer-method to determine which to use. If neither has a multimethod, it looks at their parents, walking down the ancestors until it finds one with a method.

from integrant.

weavejester avatar weavejester commented on May 18, 2024

Commit e97c4b3 should fix the dispatch of pre-init-spec.

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

Thanks! Any chance you could cut a release for this fix?

from integrant.

julienfantin avatar julienfantin commented on May 18, 2024

Awesome, thank you @weavejester

from integrant.

weavejester avatar weavejester commented on May 18, 2024

Released 0.6.2

from integrant.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.