When creating a function with recursion on Pattern matching its throws the below error, but when created with a method it works fine. Can you please help me understand.
val power: (Int, Int) => Int = (base: Int, exp: Int) => {
exp match {
case 0 => 1
case 1 => `base`
case e => `base` * power(`base`, e - 1)
}
}
The code above shows produces the error: "Forward reference extends over definition of value". But the code below works fine:
def func1(base: Int, exp: Int): Int = {
exp match {
case 0 => 1
case 1 => `base`
case e => `base` * func1(`base`, e - 1)
}
}