Scala (19): Lazy

lazy means it only be initialized for the first visit, not when its class is initialized. lazy is suitable for some scenario, for example, the cost time on initializing is very long.

Here is an example to get Scala version number from github.

class ScalaCurrentVersion(val url: String) {
  lazy val source = {
    println("fetching from url...")
  lazy val majorVersion = source.find(_.contains("version.major"))
  lazy val minorVersion = source.find(_.contains("version.minor"))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s