|
| 1 | +## Nested Methods |
| 2 | + |
| 3 | +In Scala, it's possible to define methods within other methods. |
| 4 | +This is useful when you have a function that is only intended for one-time use. |
| 5 | +For example, you may wish to implement the factorial function using an accumulator to enhance the program's efficiency. |
| 6 | +Simultaneously, you would not want to allow the user to call the function with an arbitrary accumulator parameter. |
| 7 | +In this situation, you can expose a standard one-parameter function `factorial`, which calls the nested tail-recursive implementation |
| 8 | +`fact` with the appropriate accumulator: |
| 9 | + |
| 10 | +```scala 3 |
| 11 | +def factorial(x: Int): Int = |
| 12 | + def fact(x: Int, accumulator: Int): Int = |
| 13 | + if x <= 1 then accumulator |
| 14 | + else fact(x - 1, x * accumulator) |
| 15 | + fact(x, 1) |
| 16 | +``` |
| 17 | + |
| 18 | +An alternative option is to place the `fact` function on the same level as `factorial` and make it `private`. |
| 19 | +This still permits other functions in the same module to access `fact`, whereas nesting it renders it exclusively accessible |
| 20 | +from inside the `factorial` function. |
| 21 | +You can also have nested methods within other nested methods, with the rules of scoping being consistent: the nested function is |
| 22 | +only accessible from within its outer function: |
| 23 | + |
| 24 | +```scala 3 |
| 25 | +def foo() = { |
| 26 | + def bar() = { |
| 27 | + def baz() = { } |
| 28 | + baz() |
| 29 | + } |
| 30 | + def qux() = { |
| 31 | + def corge() = { } |
| 32 | + corge() // A nested function can be called |
| 33 | + bar() // A function on the same level can be called |
| 34 | + // A function nested within the other function cannot: |
| 35 | + // baz() // not found: baz |
| 36 | + } |
| 37 | + // Functions on this level can be called... |
| 38 | + bar() |
| 39 | + qux() |
| 40 | + // ... but their nested functions cannot: |
| 41 | + // baz() // not found: baz |
| 42 | + // corge() // not found: corge |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Note that we've used curly braces to delineate scopes more clearly; however, they are not needed in Scala 3. |
| 47 | + |
| 48 | +Nested functions can access the parameters of their parents, so you can avoid passing parameters that do not change: |
| 49 | + |
| 50 | +```scala 3 |
| 51 | +def f(x: Int, y: Int): Int = |
| 52 | + def g(z: Int): Int = |
| 53 | + def h(): Int = |
| 54 | + x + y + z |
| 55 | + h() |
| 56 | + // def i(): Int = z // z is not visible outside g |
| 57 | + g(42) |
| 58 | +``` |
| 59 | + |
| 60 | +Another instance where nested methods prove particularly useful is when you create a chain of calls to higher-order |
| 61 | +functions, utilizing nested methods to assign meaningful names to their arguments. |
| 62 | +Consider the example where we count the number of kittens that are either white or ginger. |
| 63 | + |
| 64 | +```scala 3 |
| 65 | +enum Color: |
| 66 | + case Black |
| 67 | + case White |
| 68 | + case Ginger |
| 69 | + |
| 70 | +// We have a model in which any cat has a color and an age (in years) |
| 71 | +class Cat(val color: Color, val age: Int) |
| 72 | + |
| 73 | +val bagOfCats = Set(Cat(Color.Black, 0), Cat(Color.White, 1), Cat(Color.Ginger, 3)) |
| 74 | + |
| 75 | +// Count the number of white or ginger kittens (cats that are not older than 1 year) |
| 76 | +val numberOfWhiteOrGingerKittens = |
| 77 | + def isWhiteOrGinger(cat: Cat): Boolean = cat.color == Color.White || cat.color == Color.Ginger |
| 78 | + def isKitten(cat: Cat): Boolean = cat.age <= 1 |
| 79 | + bagOfCats.filter(isWhiteOrGinger).count(isKitten) |
| 80 | +``` |
| 81 | + |
| 82 | +We could have written the latter function as shown below, but it is obviously less readable: |
| 83 | + |
| 84 | +```scala 3 |
| 85 | +val numberOfWhiteOrGingerKittens = |
| 86 | + bagOfCats |
| 87 | + .filter(cat => cat.color == Color.White || cat.color == Color.Ginger) |
| 88 | + .count(cat => cat.age <= 1) |
| 89 | +``` |
| 90 | + |
| 91 | +### Exercise |
| 92 | + |
| 93 | +Explore the scopes of the nested methods. Make the code in the file `NestedTask.scala` compile. |
0 commit comments