Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'let...in' expression in Scala

Tags:

scala

In OCaml, the let...in expression allows you to created a named local variable in an expression rather than a statement. (Yes I know that everything is technically an expression, but Unit return values are fairly useless.) Here's a quick example in OCaml:

let square_the_sum a b = (* function definition *)
    let sum = a + b in (* declare a named local called sum *)
        sum * sum (* return the value of this expression *)

Here's what I would want the equivalent Scala to look like:

def squareTheSum(a: Int, b: Int): Int =
    let sum: Int = a + b in
        sum * sum

Is there anything in Scala that I can use to achieve this?

like image 358
GJK Avatar asked Dec 13 '13 14:12

GJK


People also ask

What is expression in Scala?

2.2. In Scala, all statements are expressions that return a value. The value of the last statement determines the value of an expression. The if expression is no different — it always returns a value. So, we can assign the result of an if expression to a value.

What's the difference between an expression and a statement Scala?

If you know programming in any language, you already know about statements and expressions. A statement is the smallest standalone element that expresses some action to be carried out. Whereas an expression in a programming language is something that produces or returns a value.

What is an expression following the if keyword in Scala?

Scala IF Statement The syntax of Scala if statement is if(boolean_expression) { // statements inside if block } Scala if statement contains an if keyword followed by a boolean expression. If the boolean expression evaluates to true, then the statements inside the if block are executed.

Is everything an expression in Scala?

Unlike Java, in Scala, everything is an expression. Yes, that's right.


1 Answers

EDIT:

You learn something new every day, and this has been answered before.

object ForwardPipeContainer {
  implicit class ForwardPipe[A](val value: A) extends AnyVal {
    def |>[B](f: A => B): B = f(value)
  }
}

import ForwardPipeContainer._

def squareTheSum(a: Int, b: Int): Int = { a + b } |> { sum => sum * sum }

But I'd say that is not nearly as easy to read, and is not as flexible (it gets awkward with nested lets).


You can nest val and def in a def. There's no special syntax; you don't need a let.

def squareTheSum(a: Int, b: Int): Int = {
  val sum = a + b
  sum * sum
}

I don't see the readability being any different here at all. But if you want to only create the variable within the expression, you can still do that with curly braces like this:

val a = 2                                       //> a  : Int = 2
val b = 3                                       //> b  : Int = 3
val squareSum = { val sum = a + b; sum * sum }  //> squareSum  : Int = 25

There is no significant difference here between a semicolon and the word "in" (or you could move the expression to the next line, and pretend that "in" is implied if it makes it more OCaml-like :D).

val squareSum = {
  val sum = a + b // in
    sum * sum
}

Another, more technical, take on this: Clojure's 'let' equivalent in Scala. I think the resulting structures are pretty obtuse compared to the multi-statement form.

like image 63
Rob Napier Avatar answered Oct 01 '22 03:10

Rob Napier