-1

I am new to Scala. I am trying to return a value from some scala code in Spark and I also get the function name along with the result. for ex executing below

def getHistory() : Long = {
    1 + 1
}
getHistory()

returns

getHistory: ()Long
res140: Long = 2

The additional information that is being returned seems to be a function of REPL as someone mentioned below. How can I turn this feature off so that I only see 2 being returned.

3
  • 3
    You are returning just 2, the rest is only some helpful text provided by your REPL. I'm not sure to understand your question.
    – Gaël J
    Commented Aug 2, 2021 at 11:56
  • Thank you, I am not familiar with REPL, that gives something to work with. I have modified the details. How do I configure REPL to not return the helpful text? Commented Aug 2, 2021 at 16:36
  • 3
    If you don't want the REPL text then don't run your code in the REPL. Instead, you can can put you code in a main() method, compile the file, and invoke the compiled program. Seek out a good beginners tutorial for the details.
    – jwvh
    Commented Aug 2, 2021 at 16:46

3 Answers 3

3

If you don't want to see REPL text you can set it to :silent but then you won't see anything that isn't sent to STDOUT.

Here's a sample session:

%> scala  #start the REPL
Welcome to Scala 2.13.5 (OpenJDK 64-Bit Server VM, Java 11.0.11).
Type in expressions for evaluation. Or try :help.

scala> :silent

scala> def two() = 1 + 1

scala> two()

scala> print(two())
2
scala> 
1

As the first comment on your post suggest, "the additional stuff" that you obtain is just the verbosity of the REPL.

You function indeed only return the value 2.

https://docs.scala-lang.org/overviews/repl/overview.html

1
  • Thank you, I am not familiar with REPL, that gives something to work with. I have modified the details. How do I configure REPL to not return the helpful text Commented Aug 2, 2021 at 16:37
0

The REPL cannot be configured in the exact way that you want. (Why do you even want to configure it that way? What is your motive? The question is puzzling.)

Regardless, you can use println to produce "bare" output:

scala> println(getHistory())
2
1
  • Trying to execute some scala scripts(not a jar) through certain API and then consume the result. the current API result is in the form of a json with that whole helpful text + result being returned as a string. All I need is the actual result without the helpful text. Commented Aug 2, 2021 at 19:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.