A related forth coming topic is the topic on lazy collections and projections and streams. They are all ways to lazily evaluate collections.
Note: The code for today must be put in a file and executed. Lazy val's cannot be demonstrated in the REPL because after each value is declared in a REPL the variable is accessed and it is printed out in the next line. If I considered it important enough I could have defined a method in the REPL, put the code in the method and then called the method and demonstrated it and if you wish feel free to do that. But I recommend creating a file and running scala yourfile
Examples:
- val normalVal = {
- println("---->>> Initializing normal val <<<----");
- "This is the normal val"
- }
- lazy val lazyVal = {
- println("---->>> Initializing lazy val <<<----");
- "This is the lazy val"
- }
- println ("\n\nno references have been made yet\n\n")
- println ("Accessing normal val : ")
- println(normalVal)
- println ("\n\nAccessing lazy val : ")
- println(lazyVal)
- println ("\n\nAccessing lazy val a second time, there should be no initialization now: ")
- println(lazyVal)