This page is no longer maintained — Please continue to the home page at www.scala-lang.org

classes and imports

6 replies
glob157
Joined: 2011-07-15,
User offline. Last seen 42 years 45 weeks ago.
Hi guys : I have two files 
App.scala and Utils.scala

in App.scala, i have the line 
import Utils   ;

But I get an error that a "." is expected. 
Basically, each file has one object defined in it, and i simply want the class in App.scala to see the classes in Utils.scala. 
How can I import them ?
--
Jay Vyas
MMSB/UCHC
Derek Williams
Joined: 2009-06-13,
User offline. Last seen 42 years 45 weeks ago.
Re: classes and imports

On Wed, Jul 20, 2011 at 5:53 PM, Jay Vyas wrote:
> Hi guys : I have two files
> App.scala and Utils.scala
>
> in App.scala, i have the line
> import Utils   ;
>
> But I get an error that a "." is expected.
> Basically, each file has one object defined in it, and i simply want the
> class in App.scala to see the classes in Utils.scala.
> How can I import them ?

If the classes in both files are within the same package then the
classes will be visible without importing. If you aren't declaring a
package then they are both in the top level, which means that they
should also be visible.

--
Derek Williams

glob157
Joined: 2011-07-15,
User offline. Last seen 42 years 45 weeks ago.
Re: classes and imports
This code does not compile, that is, object Test, in file A.scala, cannot invoke the run method of the B class.
Error message: ./scalacode/twitter/A.scala:9: error: not found: value B

Seems simple enough to me.... Why is B invisible ?
File 1 : 
object Test {      def main(args: Array[String])     {    println("HISDFSD"); B.run();     }    }
File 2 : 
class B{    def run()     {        println("run");    } }
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: classes and imports

On Wed, Jul 20, 2011 at 8:43 PM, Jay Vyas wrote:
> This code does not compile, that is, object Test, in file A.scala, cannot
> invoke the run method of the B class.
> Error message: ./scalacode/twitter/A.scala:9: error: not found: value B
>
> Seems simple enough to me.... Why is B invisible ?

If you change "class B" to "object B", "B.run()" will compile.

Or, change "B.run()" to "(new B).run()".

Derek Williams
Joined: 2009-06-13,
User offline. Last seen 42 years 45 weeks ago.
Re: classes and imports

Gah, I missed that one.

Derek Williams

On Jul 20, 2011 6:47 PM, "Seth Tisue" <seth [at] tisue [dot] net> wrote:
> On Wed, Jul 20, 2011 at 8:43 PM, Jay Vyas <jayunit100 [at] gmail [dot] com> wrote:
>> This code does not compile, that is, object Test, in file A.scala, cannot
>> invoke the run method of the B class.
>> Error message: ./scalacode/twitter/A.scala:9: error: not found: value B
>>
>> Seems simple enough to me.... Why is B invisible ?
>
> If you change "class B" to "object B", "B.run()" will compile.
>
> Or, change "B.run()" to "(new B).run()".
>
> --
> Seth Tisue | Northwestern University | http://tisue.net
> lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
Derek Williams
Joined: 2009-06-13,
User offline. Last seen 42 years 45 weeks ago.
Re: classes and imports

Are you using a build tool or ide? It looks like a problem with the way you are trying to run your program.

Derek Williams

On Jul 20, 2011 6:43 PM, "Jay Vyas" <jayunit100 [at] gmail [dot] com> wrote:
> This code does not compile, that is, object Test, in file A.scala, cannot
> invoke the run method of the B class.
>
> Error message: ./scalacode/twitter/A.scala:9: error: not found: value B
>
>
> Seems simple enough to me.... Why is B invisible ?
>
> File 1 :
>
> object Test
> {
>
> def main(args: Array[String])
> {
> println("HISDFSD");
> B.run();
> }
>
> }
>
> File 2 :
>
> class B
> {
> def run()
> {
> println("run");
> }
> }
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: classes and imports

On Wed, Jul 20, 2011 at 20:53, Jay Vyas wrote:
> Hi guys : I have two files
> App.scala and Utils.scala
>
> in App.scala, i have the line
> import Utils   ;
>
> But I get an error that a "." is expected.
> Basically, each file has one object defined in it, and i simply want the
> class in App.scala to see the classes in Utils.scala.
> How can I import them ?

There's a problem with the code -- see reply by Seth Tisue.

However, I think it is important to make a distinction here: Scala
does not recognize the concept of "file", except for the scoping of
"sealed" traits. Code in Scala, as in Java, is organized by packages,
which are explicitly declared in the source code.

To use code defined in "some other file", you have to take care of the
following steps:

1. Make sure you import from the package, or that you are in the same
scope (ie, same package). See information about "package" and "import"
statements for more detailed and precise information.

2. Either compile both files at the same time, or set the CLASSPATH
(through parameter or environment variable) as appropriate. CLASSPATH
should either contain the name of the JAR file containing the
resulting compilation of the file being used, or point to the base
directory of where the files were compiled to (usually called the
TARGET directory).

When you run the code, as opposed to compile it, you'll need to have
CLASSPATH set as well.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland