18

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

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102

3 Answers3

28

Well, you cannot do that really, but you can get very close to that by using package objects:

src/main/scala/yourpackage/package.scala:

package object yourpackage {
  def function(x: Int) = x*x
}

src/main/scala/yourpackage/Other.scala:

package yourpackage

object Other {
  def main(args: Array[String]) {
    println(function(10));   // Prints 100
  }
}

Note how function is used in Other object - without any qualifiers. Here function belongs to a package, not to some specific object/class.

Community
  • 1
  • 1
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 3
    That's not a function, that's a method. – Jörg W Mittag Sep 01 '13 at 12:20
  • 5
    @JörgWMittag, Of course, it is a method. Java platform does not have notion of function. Even Scala functions are just objects with fancy types. But in my answer I never said that `function` is a function, it's just a name. – Vladimir Matveev Sep 01 '13 at 15:02
20

As the other answers have indicated, its not possible to define a function without an object or class, because in Scala, functions are objects and (for example) Function1 is a class.

However, it is possible to define a function without an enclosing class or object. You do this by making the function itself the object.

src/main/scala/foo/bar/square.scala:

package foo.bar

object square extends (Int => Int) {
  def apply(x: Int): Int = x * x
}

extends (Int => Int) is here just syntactic sugar for extends Function1[Int, Int].

This can then be imported and used like so:

src/main/scala/somepackage/App.scala:

package foo.bar.somepackage

import foo.bar.square

object App {
  def main(args: Array[String]): Unit = {
    println(square(2))  // prints "4"
  }
}
Timothy McCarthy
  • 857
  • 13
  • 19
6

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.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • 2
    Do you know the design behind the scene ? In Python, function is also first class object, which CAN be defined out of any class, also ok to assigned to other variable... – Bin Nov 18 '16 at 07:17
  • 1
    @Bin I think there's talk about top-level functions in the future; current design is surely due to Java interop. – som-snytt Nov 18 '16 at 08:12