News for hackers. Stuff that matters.
Re:Printf(“%s %s”, dependency, injection) by eru on Tuesday October 19, 2021 @04:56AM (#28916146)
Go is not the only one. When I asked a few years ago, if someone could explain dependency injection in terms that I understand, ie Haskell terms, I got the reply of 'hmm, maybe it's like giving arguments to your function?'.
Re:Printf(“%s %s”, dependency, injection) by LukeShu on Tuesday October 19, 2021 @05:39AM (#28916384)
Dependency Injection is just passing arguments.

If you're writing a library that does things that involve talking to a database:

- Not DI: You `import Database.PostgreSQL.Simple` and your function(s) take the appropriate arguments to pass to postgresql-simple.

- DI: Rather than you `import`ing a library to talk to a database, you take an argument that is an object/function (a partially-applied `connectPostgreSQL`? an instance of a typeclass?) that you use to talk to the database.

(IDK if the various SQL libraries for Haskell are compatible enough with each other to make that example particularly realistic)

Instead of using a library, you take an implementation as an argument. That's all DI is.

Re:Printf(“%s %s”, dependency, injection) by nerdponx on Tuesday October 19, 2021 @06:35AM (#28916675)
Right, I think functional programmers like to trivialize the actual usefulness and value of "dependency injection" as a standalone concept.

Here's a Python example without DI:

    class MyClient:
        def __init__(self):
            self.http_client = httpx.Client()
And the DI version, which is a small change with big consequences (especially for testing; no more patching!):

    class MyClient:
        def __init__(self, http_client=None):
            self.http_client = self.http_client or httpx.Client()
Re:Printf(“%s %s”, dependency, injection) by Sohcahtoa82 on Tuesday October 19, 2021 @12:45PM (#28920232)

    self.http_client = self.http_client or httpx.Client()
should be:

    self.http_client = http_client or httpx.Client()
But yeah, once I learned that DI can be summed up into what you put here, I realized that DI is a $1,000 term for a $5 concept that has $100,000 ramifications in terms of what it enables you to do.
Re:Printf(“%s %s”, dependency, injection) by Spivak on Tuesday October 19, 2021 @03:10PM (#28922260)
Python also has a really nice pattern using classes for this. I've heard it called the supply chain pattern before.

    class MyClient(HTTPClient):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        def do_some_networking(self, arg):
            super().http_get("server", arg)

    class MyClientWithDI(MyClient, MyHTTPClient):
        pass

    client = MyClientWithDI()
    # Will call MyHTTPClient's http_get
    client.do_some_networking("/status")
Re:Printf(“%s %s”, dependency, injection) by nerdponx on Tuesday October 19, 2021 @06:51PM (#28924732)
I've actually never seen this one before, and I'm not sure if I like it. I generally tend to favor "composition over inheritance", and I'm not sure what the advantage of inheritance is in this case, compared to standard DI.
Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @08:27AM (#28929822)
Python's threads used to work like that, for example. (They still have that as an option, but I think the composition is more preferred these days?)
Re:Printf(“%s %s”, dependency, injection) by nerdponx on Wednesday October 20, 2021 @09:10AM (#28930229)
The old-school way to use Thread is to subclass it and override some methods. The post here is describing a setup where you don't override methods, but instead provide some kind of mixin. I've never seen anyone write this, for example:

    class MyThreadImpl:
        def run(self):
            do_stuff()

    class MyThread(Thread, MyThreadImpl):
        pass
Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @01:28AM (#28927084)
> Right, I think functional programmers like to trivialize the actual usefulness and value of "dependency injection" as a standalone concept.

Not sure? Eg Haskell typeclasses (and that includes monads!) could mostly be replaced by just passing arguments around (basically a bit like a vtable in C++). But it would make the ergonomics almost unusable.

Of course, many real world functional programmers (or wanabe functional programmers) like to make fun of OOP.

Re:Printf(“%s %s”, dependency, injection) by gpderetta on Tuesday October 19, 2021 @06:23AM (#28916625)
and passing a factory is just call-by-name/call-by-need.
Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @01:28AM (#28927089)
Well, factories are really just functions.
Re:Printf(“%s %s”, dependency, injection) by emmab on Tuesday October 19, 2021 @05:23AM (#28916296)
Re:Printf(“%s %s”, dependency, injection) by Athas on Tuesday October 19, 2021 @05:41AM (#28916398)
While this is true, it should be noted that:

* Implicit parameters are a language extension, not part of standard Haskell (although extensions are so ubiquitously used that this does not matter much).

* Implicit parameters are rarely used[0]. I would not be surprised if most experienced Haskell programmers have never used them, and while they may know they exist, they might not even remember the syntax or semantics. I belong to that category myself. In Haskell, type classes tend to be used for what other languages might do with implicit parameters.

[0]: https://gist.github.com/atondwal/ee869b951b5cf9b6653f7deda0b...

Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @01:30AM (#28927099)
In more orthodox Haskell, you can use eg the 'Reader Monad' with a suitable datastructure (or combination of Applicatives) where other languages would use dynamically scoped variables.
Re:Printf(“%s %s”, dependency, injection) by gpderetta on Tuesday October 19, 2021 @06:26AM (#28916633)
Nice! I was wondering if implicit parameters were equivalent to dynamic scoping. It seems to be the case.

more on the topic:

http://blog.ezyang.com/2020/08/dynamic-scoping-is-an-effect-...

Re:Printf(“%s %s”, dependency, injection) by jcelerier on Tuesday October 19, 2021 @05:38AM (#28916381)
> Go is not the only one. When I asked a few years ago, if someone could explain dependency injection in terms that I understand, ie Haskell terms, I got the reply of 'hmm, maybe it's like giving arguments to your function?'.

not sure it's a really good explanation. You aren't the one giving the arguments to the function, it's the framework in which you are opting into which is going to do that.

Imagine you are writing a software for, say, rescaling images. You have hard requirements:

  - You want people to be able to provide their own rescaling code as external plug-ins / DLLs which you do not know about when compiling your host. 
  - You want your host software to provide the memory allocation primitives to the DLLs so that they can allocate memory for the rescaled images in a way that you will have control of in the host software. 
  - The memory allocator being used can itself be a plug-in too, that the user can choose at runtime in e.g. a GUI settings panel.
How do you do that ? I'd wager that whatever solution you find, it will really look like DI.
Re:Printf(“%s %s”, dependency, injection) by jppittma on Tuesday October 19, 2021 @05:55AM (#28916481)
I’ll hold the rescale image function as a function pointer and only call vía that pointer when I want to rescale. If you give me a new one via a plug-in or dll, I’ll set the value of the rescale function to your function.
Re:Printf(“%s %s”, dependency, injection) by jcelerier on Tuesday October 19, 2021 @08:22AM (#28917301)
Yes, and you are going to store the function pointers somewhere in your code right ? To show a GUI menu to your users with all the available plugins
Re:Printf(“%s %s”, dependency, injection) by im3w1l on Tuesday October 19, 2021 @05:51AM (#28916454)
The old school way of doing that is to have a "plugins" folder. Every library there is loaded. It calls a register-library function and provides a struct full of callbacks (for image rescaling or memory allocation in this case). The plugin directly calls memory allocation routines of the host, which delegates to the one selected in the ui.
Re:Printf(“%s %s”, dependency, injection) by jcelerier on Tuesday October 19, 2021 @08:21AM (#28917291)
> Every library there is loaded. It calls a register-library function and provides a struct full of callbacks

Congrats on reinventing DI ! Guess what your array of struct reacale_plugin* is called, hint, it starts with DI and ends with container

Re:Printf(“%s %s”, dependency, injection) by matheusmoreira on Tuesday October 19, 2021 @09:08AM (#28917651)
I'm not sure anyone is reinventing anything though. This function pointer stuff is so simple I don't think anyone ever bothered to give it a name before. Even the Linux kernel is full of this stuff, kernel modules are literally this and I've never read a single comment talking about dependency injection.

People invented this because Java and its friends made it so insanely hard to do it that you needed to invent convoluted techniques to accomplish the same task.

Re:Printf(“%s %s”, dependency, injection) by jcelerier on Tuesday October 19, 2021 @10:39AM (#28918591)
> Even the Linux kernel is full of this stuff, kernel modules are literally this and I've never read a single comment talking about dependency injection.

here's one: https://stackoverflow.com/questions/5925270/how-to-do-depend...

Re:Printf(“%s %s”, dependency, injection) by matheusmoreira on Tuesday October 19, 2021 @01:00PM (#28920438)
That's a stackoverflow question, not Linux kernel source code. Looks like everyone there is realizing the fact dependency injection is just a simple technique that's been around for a long time. Just like I said. Mocks? Just provide a different function. It's easy.
Re:Printf(“%s %s”, dependency, injection) by im3w1l on Tuesday October 19, 2021 @09:33AM (#28917897)
This is unneccesarily sarcastic.
Re:Printf(“%s %s”, dependency, injection) by eru on Tuesday October 19, 2021 @05:43AM (#28916408)
Well, naively this would sound to me like you would pass these things in as arguments?

(Alternatively, for something more Haskell specific, you can also write your stuff generically against a typeclass, and let people provide their own implementations.)

Re:Printf(“%s %s”, dependency, injection) by agumonkey on Tuesday October 19, 2021 @06:23AM (#28916620)
In FP everything is an argument anyway, but in OO land .. you always have to come up with a new concept to overcome the many limits.

That said, I think Scala did that, separating module/package level parameters and functional parameters can help designing/thinking.

my 2 cents

Re:Printf(“%s %s”, dependency, injection) by eru on Wednesday October 20, 2021 @01:32AM (#28927109)
In FP, we still come up with lots of new concepts. Especially when it comes to statically typing these 'just an argument anyway' constructs.

But yeah, you probably get something like 80% of the benefit of modern functional programming from just first-class functions, algebraic data types (and pattern matching over them), and immutability by default.

Re:Printf(“%s %s”, dependency, injection) by ngc248 on Tuesday October 19, 2021 @06:27AM (#28916641)
DI is just passing arguments, but there is another term used along with that, which will better clarify why DI is needed, which is IOC (Inversion of Control).