Scala claims than OO and FP can be combined. I wonder how this can be achieved in practice. I mean object can change, so making them immutable means i have to create a new object whenever something changes right? This doesn't seem too effective to me.
By the way, if i make external reference to an object property from a function, doesn't it hurts referential transparency?
1 Answer 1
Don't think of this as one paradigm imposing restrictions on the other but as how can one take the best of both paradigms.
As a simple example:
Objects have functions which can be internal to an object. Now the internal functions can be immutable within an object and those results of a function can be used to change the state of a object.
Thinking at a different level one can use functions to create a library that can be used by objects.
How I like to make the best of both is I tend to make libraries (modules) for the more abstract processing using a functional language and then use OO languages for the layers closer to human and external processing. This is not a hard and fast rule but a guideline from where I start.
-
By internal function you mean method :)gabox01– gabox012014年03月28日 12:23:02 +00:00Commented Mar 28, 2014 at 12:23
-
Do you mean that if a pure function takes an object as parameter, the first thing it should do is to clone it?gabox01– gabox012014年03月28日 12:24:22 +00:00Commented Mar 28, 2014 at 12:24
-
By internal function you mean method. Yes.Guy Coder– Guy Coder2014年03月28日 15:07:45 +00:00Commented Mar 28, 2014 at 15:07
-
Do you mean that if a pure function takes an object as parameter, the first thing it should do is to clone it? No. Think of it like this; the function would pull the object off the stack, change it, then store the result (possibly a modified object) on the stack.Guy Coder– Guy Coder2014年03月28日 15:10:32 +00:00Commented Mar 28, 2014 at 15:10