3

Hello I have this scala object and I want to run the code in sample function using the shell with scala:

object SampleObject{ 
    def sample(){

         val data = Array(1, 2, 3, 4, 5)
         data.foreach(println(_))
    }
}

What I do is this

scala> :load /Users/username/Desktop/Cli.scala 
Loading /Users/username/Desktop/Cli.scala...
defined object SampleObject

But if I run this

scala> SampleObject.sample

This is what I ge this

<console>:92: error: value sample is not a member of object SampleObject
   SampleObject.sample

Why does this happen? There's a problem with the commands :load and -i, it's like you can't access to the objects members. Any suggestion to make it work?

UPDATE1: If I run the code above neither doing load or -i but by writing it directly on the shell everything works properly.

UPDATE2: Still not working... enter image description here

9
  • Am I right assuming you compile your Scala file before loading it in Spark? By the way, what version of Spark and Scala are you using? Commented Mar 31, 2017 at 13:25
  • Works fine for me using sbt console Commented Mar 31, 2017 at 13:54
  • @asettouf it says "Scala code runner version 2.11.7". Anyway I didn't compile it, I've loaded it and it rightly says "defined object SampleObject" so why can't I access to it's members? Commented Mar 31, 2017 at 16:00
  • @radumanolescu lucky you, on my terminal this doesn't work. Wether I call ">scala -i /dir/file.scala" or I do ">scala; >:load /dir/file.scala" it says defined object SampleObject but it doesn't allow me to access to its members anyway. Commented Mar 31, 2017 at 16:04
  • Actually my bad, no need to compile. I just made the test with Scala 2.11.8 and Spark 2.1.0 and it works fine. (both with -i and :load) Meaning there is something else going wrong here. What Spark version do you use? What jdk version are you using as well? Commented Mar 31, 2017 at 16:54

1 Answer 1

4

The problem is that line break before the brace. You can fix this either of two ways:

  1. Use :paste instead of :load
  2. Put the brace on the same line as the object name

Your original question said object SampleObject{ but it turns out that is not what your actual file contains, as your update 2 shows. Since :load works line-by-line, just as if you were typing at the REPL, when the interpreter sees

object Cli
it's done: that is a perfectly valid, complete Scala declaration of a (not terribly interesting) object. The opening brace on the next line will start a new block that is unrelated to the object you've just defined.

This is an area where the interactive REPL behaves differently from the compiler, which will allow a single newline before the brace (see the Scala language specification for specifics).

Sign up to request clarification or add additional context in comments.

1 Comment

Oh that's true! That was my fault too with the question! Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.