3 ms·
The problem dependency injection attempts to solve disappears when you have dynamic scoping. I've said this before - DI is probably the dumbest fad (no small fe
by sedachv 17y ago
The problem dependency injection attempts to solve disappears when you have dynamic scoping. I've said this before - DI is probably the dumbest fad (no small feat) in object-oriented programming.
- Tichy 17y agoCould you remind me what dynamic scoping is? Thanks!
- viraptor 17y agof() { return x; } g() { x=1; return f(); } f() returns x it inherits from g(). A spectacular way to shoot yourself in the foot.
- Tichy 17y agoThat looks like no scoping at all? I think JavaScript is a bit like that? But most languages would prevent that kind of thing?
- regularfry 17y agoIt's not no scoping - it's specifically scoped to where the call to f() was made, rather than where it was defined. It basically makes everything in scope in g() available to f() as a hidden parameter. Pre-CL Lisps had this by default. defvar is, as far as I can tell (still learning!) how you get at it now.
- Tichy 17y agoThanks for the info. At first sight it looks impossible to handle (how do you know what variables f expects), but maybe there is a technique to it I don't know yet.
- sedachv 17y agoWrong on all counts. Dynamic scoping depends on having a construct to introduce new scopes (a fancy way of saying 'let statement'). Here's what the classic problem of "how do I test my code using a fake service?" would be solved with dynamic scoping: (defvar service <default service>) ;; in Common Lisp you have to declare that certain variables will have dynamic scope, otherwise they would be lexical (defun some-function () (use service)) (defun test () (let ((service <fake-test-service>)) (do-something (some-function)))) This is more than global variables, because now you can plug in any service you want in any runtime context you need without affecting other code. If this looks a bit like AOP, it's because applying dynamic scope to functions is the actual underlying mechanism behind AOP: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.113.8305&rep=rep1&type=pdf http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.113...