Re:Goodbye, shitty "Car extends Vehicle" object-orientation ...
by sassafras on Monday August 22, 2011 @11:42PM
(#2915249)
Interesting. I suppose when I think of DI I usually think of Java. Do you know of a good example of the DI pattern being used in functional languages?
Re:Goodbye, shitty "Car extends Vehicle" object-orientation ...
by fleitz on Tuesday August 23, 2011 @12:00AM
(#2915293)
map and reduce. DI is so prevalent in functional programming that it's pretty much taken for granted.
Re:Goodbye, shitty "Car extends Vehicle" object-orientation ...
by sassafras on Tuesday August 23, 2011 @12:22AM
(#2915327)
Well, take this Clojure tutorial, for instance, comparing a simple DI implementation in Java with a single function in Clojure. The latter bears almost no resemblance to the former, other than achieving the desired effect. Is it fair to say that the user of a closure is implementing the Dependency Injection pattern, or is it a positive side effect of the fundamental properties of first class functions and lexical scope?
Re:Goodbye, shitty "Car extends Vehicle" object-orientation ...
by fleitz on Tuesday August 23, 2011 @02:58PM
(#2917891)
IMHO it's largely a side effect of first class functions. In Java in order to pass a function you need to pass an object that has the function, or an interface with the function, so it leads to a lot of line noise and thus people don't do it much. DI doesn't look impressive in a functional language because it's so well supported, but if you look at something like a global accumulator in a functional language you'll need a monad because you need state.
eg in Java it's easier to write:
int accum = 0;
for(int i : collection){
accum +=i;
}
rather than collection.reduce(new IReduce<int> {
int reduce(int accum, int i){
return i + accum;
}
})
I'm a bit rusty on Java so the syntax may be off but you get the idea, where as in a functional language (F#) that code is reduced to: collection
|> Seq.reduce +
Or in an imperative language that supports function passing (C#) collection.sum((a,b) => a+b)
> concepts such as Dependency Injection, which was the meat of the grandfather rant, are IMHO more an outgrowth of dealing with the limitations of OO design...
I don't think so. The inflexibility you can loosen with DI exists in all kinds of non-OO and even non-imperative programs as well. In a sense, the whole point of object-orientation is that it gives you a handle on that kind of thing: the thing you depend on is an object reified at run-time, which you can arrange to have passed in to you instead of extracted from a global namespace, and which can be replaced with some other object with different behavior, not an address you are jumping to that's hard-coded into your compiled jump instruction as an immediate argument.