3

I am new to scala. I have created a scala object containing a couple of methods. I am trying to invoke the method from Scala REPL but the import statement is not working.

This is the code I tried (It is in a default package):

object Hello extends App {

    def sum(xs: List[Int]): Int = 0;

    def max(xs: List[Int]): Int = 0;

}

For starting the Scala REPL, I opened the console and then did the following:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\user>sbt
[info] Set current project to user (in build file:/C:/Users/user/)
> console
[info] Updating {file:/C:/Users/user/}user...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_2
2).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import Hello._
<console>:7: error: not found: value Hello
       import Hello._
              ^

scala>

The project workspace and the project is created in E:/Work. Even I tried navigating to the project and then followed the same steps above but it gives the same error.

Please let me know how to make this import statement work from Scala REPL.

user182944
  • 7,897
  • 33
  • 108
  • 174
  • Where exactly the file is located? It's good to be in `${PROJDIR}/src/main/scala` – Odomontois Nov 12 '15 at 05:17
  • Did you run your `console` **after** writing the source? – Odomontois Nov 12 '15 at 05:20
  • @Odomontois I am not following the directory structure you mentioned. The `Hello.scala` file is placed under the src folder (Default Package). Can you please guide how to proceed? – user182944 Nov 12 '15 at 05:46
  • This is the problem. Place your source under the root project folder or to `src/main/scala`. And always run `sbt` from project directory – Odomontois Nov 12 '15 at 06:17
  • In addition to @Odomontois' answer, see http://stackoverflow.com/questions/9822008/why-is-my-object-not-a-member-of-package-root-if-its-in-a-separate-source-fil. Put the object into a package. – Alexey Romanov Nov 12 '15 at 06:21

2 Answers2

2

For project structure please refer to this page

Your dir tree should be something like

E:
|
`- Work
   |
   `- {PROJECT}
      |
      +- build.sbt
      |
      `- src
         |
         `- main
            |
            `- scala
               |
               `- Hello.scala

Next navigate to E:\Work\{Project} and from there run sbt and only then console and do all the work

Remember that your REPL will refer to classes that were compiled before console were executed.

Note that your project structure could be much simpler like

E:
|
`- Work
   |
   `- {PROJECT}
      |
      `- Hello.scala

But this is not recommended

Odomontois
  • 15,918
  • 2
  • 36
  • 71
  • I manually created the directory structure and it worked like a charm :) – user182944 Nov 12 '15 at 06:25
  • If in case I want to compile the classes after `console` command from command prompt i.e. from `scala>` then do we have some command to compile it? Or every time, in case of a code change, from `scala>` I have to move out using `Ctrl+D` and then type `console` for compiling the newly changed code ? Please let me know – user182944 Dec 29 '15 at 12:23
0

you need to compile your project

exit the scala REPL, then from the shell prompt:

> sbt clean compile package

the compile task is a dependency on the sbt, so just sbt is the same as sbt compile, but i don't see a line in your console output that shows it compiled your project (it will begin with Compiling)

alternatively if you have no dependencies (which it looks like you don't) then you can just paste that code directly in the REPL session, and let the Scala REPL compile your code for you, like so:

scala> :paste
// paste your code in the REPL window
scala> ctrl-D
doug
  • 69,080
  • 24
  • 165
  • 199
  • Actually by default `console` task depends on `compile`. And there is nothing to do with `package` – Odomontois Nov 12 '15 at 05:19
  • @Odomontois: wrong--'console' brings up the scala REPL not the sbt console; so the OP first needs to exit the scala REPL before any sbt tasks can be executed. Second, yeah, we know, 'compile' is a dependency for 'console' but look at his console output, do you see a line having anything to do with compiling? eg, "[info] Compiling 1 Scala source"; It's not there. The package task just puts your classes in a jar, as i said, it's the compile part that's important – doug Nov 12 '15 at 05:34
  • This output goes from sbt in wrong dir. About exiting - OP need to exit from console, not from sbt. Your command includes sbt run with several tasks, so either you are suggesting exit from sbt also or your command is wrong – Odomontois Nov 12 '15 at 05:40