lazy function definitions in scala

后端 未结 6 1515
滥情空心
滥情空心 2021-02-02 16:49

I\'ve been learning scala and I gotta say that it\'s a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascri

6条回答
  •  死守一世寂寞
    2021-02-02 17:31

    I think some of the responders were a little confused by the way you phrased the question. The Scala construct you want here is a simple lazy definition:

    lazy val foo = new java.util.Date
    

    The construction of the Date object will occur at most once and be deferred until the first reference to foo.

提交回复
热议问题