I just started learning Scala. I am quite satisfied with the design of OO, and even more so with functional programming; although, I programmed long enough so that FP is not completely unnatural for me. From the first day of my Scala adventure, I have had this, letβs say, inconvenience with the obvious dialectic that occurs between OO and FP. It is clear that everyone can go one way or another. My first tendency was to consider classes as their packages, which combine functions that I want to transfer, which balances the scales on the functional side. I feel that there must be a better way to balance the action. I am also not sure how to proceed with some familiar situations within this scenario. For example, if I had the following (artificial) class:
class ValueGenerator { def value() = { "1" } def value(line: String) = { line } }
in OO programming, I would call value correct signature when I needed to get the result I needed. Methods have the same signature, because they logically correspond to similar actions. In OO, I would pass a reference to the object, and the methods that receive the ValueGenerator object will make a call to the right value , depending on the situation. As far as I can see, at least this is my tendency that a method should pass the norm in Scala. But in this case, although the methods do the same, they do not have the same signature, so they cannot be replaced by each other (or can they?). In other words, can the sender method decide which function to send regardless of the signature of the function? This seems unlikely because the recipient does not know how to call it. What is the right action in such a situation. Or does one go with gut instinct? Is there a rule of thumb you follow when it comes to OO vs FB?
As a note, it was interesting to see that my friend, who also studies Scala, had precise thoughts (or lack thereof) like me on this issue.
oop scala functional-programming
delmet
source share