by pka · 9y ago · 0 comments
I think OOP is a better way to build an ORM. That said, the query constructors might benefit from a FP approach.
I think FP is a better way to build a mathematics library. That said, data structures might be better represented as objects.
It really depends on the specific application.
I also want to clarify that OOP does not necessarily require inheritance and such.
data Address = Address
{ city :: String
, street :: String
}
data Person = Person
{ name :: String
, address :: Address
}
My general impression of recent-ish OOP developments is that people have started preferring composition over inheritance anyway, for good reasons. I guess I should've framed by question more like "the merits of inheritance over composition".Some problems are just more inherently imperative and stateful. I use FP code when it makes sense, and I use imperative code when it makes sense.
There Is No Silver Bullet.
data Data = Data { thing :: Int, that :: Boolean }}
addToThing :: Int -> State Data Int
addToThing n = do
x <- get thing
modify (data -> data {thing: x + n})
return x
It corresponds to this Typescript code: class Data {
constructor(
private thing: number,
private that: Number
) {}
addToThing(n: number) {
const old = this.thing
this.thing = old + n
return old
}
}
Except for the fact that you only need to see mutation if you want to, and you certainly can't use the mutation unless you want it.To me it doesn't really look like jumping through hoops, but we're up front about the mutation so no-one's ever going to get a surprise.
There's no silver bullet, sure. But I don't think pure FP is really all that complex. It's mostly just different.
But I actually wasn't asking about pure vs imperative, but rather FP vs OOP. I haven't heard of a pure OOP language, but in that context I think those properties could be seen as more or less orthogonal.