In Scala, how would you declare static data inside a function?

Viewed 5062

In many situations I find that I need to create long-living values inside a function's scope, and there is no need for this data to be at class/object scope.

For example,

object Example {

   def activeUsers = {
       val users = getUsersFromDB  // Connects to the database and runs a query.
       users.filter(_.active)
   }
}

Above, the variable users is in the correct scope, but it will execute a database query everytime the function activeUsers is called.

To avoid this, I could move the variable users outside the function's scope:

object Example {
   val users = getUsersFromDB  // Connects to the database and runs a query

   def activeUsers = {
       users.filter(_.active)
   }
}

But that makes it available to other functions as well.

Else, I could create a separate object to enclose the function:

object Example {

   object activeUsers {
       val users = getUsersFromDB  // Connects to the database and runs a query.

       def apply() = {
           users.filter(_.active)
       }
   }
}

But this involves more boilerplate code, use of another object and slight syntax oddities related to apply.

  • Is there support for something like this at the language level?
  • If not, is there any standard technique that you use in this situation?
2 Answers
Related