That's how the real world is. The state and the functionality are in one thing. OOP models the real world. Are you suggesting that data should be stored in global variables instead?
You don’t lose scoping by avoiding classes. I’m not really sure what you are asking. You store all data in primitives or structs, and pass it along to functions, which mutate the data. Much simpler to reason about, comprehend, debug, test, and so on.
class MyStuff {
int myState;
void myFunc() {
...
}
}
This is somehow "encapsulated". But start calling myFunc() at different times from different threads and see if myState behaves more like a stack variable or a global variable.
OO made it much more socially acceptable to pollute your code with this kind of "encapsulation". And OO devs decided that even this was too much of a straightjacket - some people liked C#'s take on properties, where you didn't have to work so hard writing getters and setters (further breaking encapsulation).
Once you take away inheritance ("prefer composition"), polymorphism (better offerings elsewhere), encapsulation (not encapsulation!), message-passing (better offerings elsewhere), you're left with a small syntactic trick that really wasn't that hard to do in C.
Even Lua gives you that [1]:
> This use of a self parameter is a central point in any object-oriented language. Most OO languages have this mechanism partly hidden from the programmer, so that she does not have to declare this parameter (although she still can use the name self or this inside a method). Lua can also hide this parameter, using the colon operator.
function Account.withdraw (self, v)
self.balance = self.balance - v
end
function Account:withdraw (v)
self.balance = self.balance - v
end
Uh no. You would typically retrieve the data in a function and assign it to a variable scoped to the function. The struct or array then gets passed through a pipeline of functions to do something possibly writing the data back to disk or the db.
Even if there is some data I want to keep in memory there is no need for a global usually. In go I may just keep it in a var in a goroutine that doesn’t exit until the process does.
> Are you suggesting that data should be stored in global variables instead?
According to FP, data shouldnt be stored where possible and yes, if you can, store global or top level immutable blobs and pass by reference.
> That's how the real world is.
Well, maybe not. I dont rememer where it came to me that OOP is like classical physics, where everything has to get passed around with the speed of light and FP is like quantum physics, stateless until you do the computation. I dont know how accurate this analogy is but it still fascinates me since modelling reality is enlarge what programming is.