MainstreamDeveloper00 MainstreamDeveloper00 - 3 months ago 16
Scala Question

Can I define and use functions outside classes and objects in Scala?

I begin to learn Scala and I'm interesting can I define and use function without any class or object in Scala as in Haskell where there is no OOP concept. I'm interested can I use Scala totally without any OOP concept?
P.S. I use IntelliJ plugin for Scala

Answer

No, functions as "first-class objects" means they are still objects.

However, it is still easy to construct a Scala application that does nothing but invoke a function that is arbitrarily composed.

That said, I can't even say that I have told you something meaningful.

Edit for glibness:

One Odersky meme is that Scala's fusion of FP and OOP exploits modularity of OOP. It's a truism that Scala doesn't strive for "pure FP"; but OOP has real consequences for everyday programming, such as that a Map is a Function, as is a Set or a String (by virtue of enrichment to StringOps).

So, although it's possible to write a program that just invokes a function and is not structured OOPly, the language does not support active avoidance of OOP in the sense of exposure to inheritance and other unFP things like mutable state and side effects.

Maybe someday they'll add a lint option: -Xlint:no-oop.

Comments