Open In App

Scala – Expression that Can’t be Reduced to a Value

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Scala adheres to expression-centric philosophy design, where anything can be evaluated to produce value. This is a way to be more expressive and follow the principles of functional programming.

Substitution Model in Scala

The substitution model is a fundamental concept in programming. It provides a way to understand how expressions are evaluated. In this model, start by breaking down expressions into smaller sub-expressions and evaluating them individually. This is done by replacing function arguments with their corresponding values, followed by evaluating the value of the function, repeated until we reach a final result.

  1. The process is the process repeated until all the variables are assigned with their values. This model is beneficial for understanding how evaluation proceeds in functional programming languages like Scala.
  2. As long as the expressions themselves are pure and do not have any side effects (modifying things that are out of their scope), the Substitution model stays consistent.
  3. This encourages writing pure functions, allowing immutability.
  4. Additionally, Scala’s type system and extensive support for higher-order functions build on top of the principles of lambda calculus.

An expression that can’t be reduced to a value:

Scala




object Main
{
    def main(args: Array[String])
  {
        def loop: Int = loop
    }
}


Explanation:

In the above code we are defining a function whose value is declared as itself. So, when the code is run, it starts by trying to evaluate the value of function loop, it’s value is decarled as loop, so it then makes a function call to the function loop, which itself creates another function call.

20240211-17_23_31

Function calling itself

This is recurvise function calling, for which there is no termination defined. Hence this is a non terminating recursive function.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads