Search code examples
scalafunctional-programmingscala-2.10

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


Solution

  • 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.

    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.