0

I am learning Scala and came across a program where Value s in the program is mentioned just s. I mean it is not println(s) or something else. Why do we mention it like this? What does it do? You can find this in the 10th line of the code below...just s declared as val.If it is supposed to write the value of s, then why don't we use println(s)?

object Main {
  class MyString(val jString: String) {
    private var extraData = ""
    override def toString = jString + extraData
  }

  object MyString {
    def apply(base:String, extras:String) {
      val s = new MyString(base)
      s.extraData = extras
      s
    }

    def apply(base:String) = new MyString(base)
  }

  def main(args: Array[String]) {
    println(MyString("hello", "world"))
    println(MyString("hello"))
  }    
}
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
adit
  • 95
  • 2
  • 10
  • 1
    This is instead of writing return s. Flagged as duplicated. Feel free read scala documentation on SO: https://stackoverflow.com/tags/scala/info – Pavel Sep 26 '17 at 18:59
  • 1
    Possible duplicate of [Return in Scala](https://stackoverflow.com/questions/12560463/return-in-scala) – Pavel Sep 26 '17 at 19:00
  • `println(s)` evaluates the expression `println(s)`. `a + b` evaluates the expression `a + b`. So, what do you think `s` does, by analogy? – Jörg W Mittag Sep 27 '17 at 22:06
  • @JörgWMittag. By analogy, I think it just expresses/evaluates value of s. If so, what is the need because I think it is already doing it in a line just above this one. – adit Sep 28 '17 at 15:00
  • 1
    Yes, that's exactly what it means. It evaluates to whatever object is bound to the variable `s`. The line above is completely different. It calls the `extraData_=` method of `s`. Evaluating `s` and calling a method on `s` are not the same thing, they are completely different. – Jörg W Mittag Sep 28 '17 at 18:30

2 Answers2

2

In Scala, the last expression of a function is what is returned. Consider a function that adds two strings together with a space in the middle like this:

def stringAdd(s1: String, s2: String): String = {
  s1 + " " + s2  // This is returned
}

In that function it is easy to see that the result of the string addition is what is being returned.

Sometimes you need to specify manually what expression you want to return. This can happen when you have side-effects at the end of your function. For example, lets say we want to add some logging for each result of our function:

def stringAdd(s1: String, s2: String): String = {
  val result = s1 + " " + s2
  Logger.info(s"stringAdd: $result")
}

The above code is incorrect, because the return value would be the value of the last expression, which is Logger.info. So we have to capture the result of our addition into a variable (result which is like your s) and put our result as the "standalone" final expression of the function:

def stringAdd(s1: String, s2: String): String = {
  val result = s1 + " " + s2
  Logger.info(s"stringAdd: $result")
  result // This will be returned
}
Tyler
  • 17,669
  • 10
  • 51
  • 89
0

I got the answer from Jorg W Mittag comments above

adit
  • 95
  • 2
  • 10