Expression block can be nested in other expression block. If both inner expression block and outer block has the same variable name then the variable defined in inner block overrides outer block.
scala> var msg = {
| var a = 10;
| println("Outer Block : a = " + a);
| {
| var a = 11;
| println("Inner Block : a = " + a);
| }
| a + 123
| }
Outer Block : a = 10
Inner Block : a = 11
var msg: Int = 133
For example, I defined variable ‘a’ in both outer expression block and inner expression block. From the output, you can confirm that the variable defined in inner block overrides outer block variable.
Note
a. Functions in Scala are named and reusable expression blocks.
b. Since nested expression blocks is possible, same is the case for nested functions also.
No comments:
Post a Comment