3

There are some options to inject dependencies in FP. I want to compare here only the two:

  1. Injection via parameter list

  2. Injection via Reader

The 2nd case is more composable and less verbose, in case I invoke several methods with dependencies. It lets me pass a dependency once. But I still do not feel it, or do not have exact rules, when it is better to pass via Reader or not.

For instance, passing external dependency to a service, is more convenient via Reader. But for instance, an instance of Map or some DTO/case class objects, which can be considered just as a holder of some input data attributes.

During my current experience, I've found for instance, that when readers with different set of dependencies are used, it is getting more verbose to transform them to each other. And sometimes a code does not look clear.

I know, such questions are not good for [stackoverflow.com], but I believe this issue is not subjective and certain arguments can be given to choose a correct option.

Alexandr
  • 9,213
  • 12
  • 62
  • 102
  • 1
    [Related?](http://degoes.net/articles/zio-environment) – Markus Appel Jun 11 '19 at 08:39
  • 1
    Interesting article, thank you. But it describes option how to improve the 2nd option of DI applied via readers. And it is for sure very important to understand it. Exact question in my topic is: can I specify rules/context, when passing dependency as a parameter is a preferable option, compared to readers? Or, we should always use readers and as it is described in the article, improve its usage with so called zio-environment. Or probably other ways to improve it and simplify. – Alexandr Jun 11 '19 at 13:08
  • When using context bounds (I presume that's what you mean) you should look into **tagless final** programming. It has a big community that practices this paradigm almost religously. The article describes tagless final with all its pros and cons, that's why I deemed it useful for your question. But the author states that his method has some distinct improvements over tagless final. But everyone can judge that for themselves. – Markus Appel Jun 11 '19 at 13:35
  • The article further states that a classic `Reader`-based approach is suboptimal: "There is a Reader monad transformer that can add the reader effect to any base monad, including the IO monad. However, not only are monad transformers very slow in Scala (adding 2-4x overhead per layer), but they have clumsy ergonomics and bad type inference." – Markus Appel Jun 11 '19 at 13:38

0 Answers0