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

后端 未结 2 570
予麋鹿
予麋鹿 2020-12-25 12:35

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 ex

2条回答
  •  猫巷女王i
    2020-12-25 13:10

    Extending FunctionXX is another way of achieving the goal; it might have an advantage of providing better documentation. Both parameter types and return value type are visible on the first line of the declaration:

    val activeUser = new Function0[List[String]] {
      val users = getUsersFromDB
      def apply = users filter (_.active)
    }
    

提交回复
热议问题