Is it possible to define functions inside other functions in Scala?
Explanation
Scala allows you to define functions inside a function and functions defined inside other functions are called local functions. Example of factorial with local function:
def factorial(i: Int): Int = {
  def fact(i: Int, accumulator: Int): Int = {
    if (i <= 1)
      accumulator
    else
      fact(i - 1, i * accumulator)
  }
  fact(i, 1)
}
Like a local variable declaration in many languages, a nested method is only visible inside the enclosing method. If you try to call fact() outside of factorial(), you will get a compiler error.
Source: Scala Nested Functions

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Scala
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты