On Saturday, 30 September 2023 at 12:40:29 UTC, Guillaume Piolat wrote:
On Friday, 29 September 2023 at 16:56:47 UTC, Imperatorn wrote:

Sounds a bit like dependency injection but for state

Possibly, I'm not familiar with dependency injection.
When is it useful?

Dependency injection is a principle of making your classes/functions self-contained and isolated, it means that when your code might need to create a resource (such as open a file to write data) it is instead up to the caller to provide that resource, but your code never does that by itself because a library can't possibly know the environment and restrictions of the target system/machine.

Simply put, you can't possibly know how to open a file in that system, you can't possibly know what allocator is used in the caller environment (think about very low-level or bare metal program), and so on, so instead caller must provide everything that your function/method/class might need to do the work.

In the most complex situations where the entire program graph is about to be created in the main function there is so called DI containers that configures all this stuff in one central place.

Reply via email to