4 ms·
> 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 re
by jcelerier 5y ago
> 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.
- jppittma 5y agoI’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.
- jcelerier 5y agoYes, 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
- eru 5y agoWell, 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.)
- im3w1l 5y agoThe 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.
- jcelerier 5y ago> 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
- matheusmoreira 5y agoI'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.
- jcelerier 5y ago> 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-dependency-injection-in-c/10863181#comment40791186_5925270 https://stackoverflow.com/questions/5925270/how-to-do-depend...
- matheusmoreira 5y agoThat'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.