• bjornsno@lemm.ee
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    Thought you were OP for a second there, as they were talking about composability. Whether it’s dependency injection or not depends on what shape your parameters take. If you’re doing functional programming and you’re passing handlers and connections etc. as params, that’s dependency injection. If you’re only passing strings and objects and such and the function has to do a bunch of logic to decide how to handle its params, that’s not dependency injection.

    • dreugeworst
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      I think the main reason OOP has a well-known term and pattern for dependency injection is to differentiate these two (out of multiple) options:

      • the constructor of my object creates other objects it depends on itself
      • I construct the dependencies of my object elsewhere and pass them in to the constructor and use an interface to make it easy to swap behaviour

      However, this becomes less of a pattern in functional programming as you wouldn’t make such objects to begin with. In FP, you pass all parameters where a function is invoked, and DI just becomes using generic parameters. You wouldn’t instantiate a dependency on each function call after all.

      As this is such a minor change, it’s not really talked about much and it’s not really a pattern,