Reputation: 751
I am a beginner in Scala coming from Java background. I have two singleton object definition in two files as below.
LongLines.scala
import scala.io.Source
object LongLines {
def processFile(filename: String, width: Int) {
val source = Source.fromFile(filename)
for (line <- source.getLines)
processLine(filename, width, line)
}
private def processLine(filename: String,
width: Int, line: String) {
if (line.length > width)
println(filename +": "+ line.trim)
}
}
FindLongLines.scala
object FindLongLines {
def main(args: Array[String]) {
val width = args(0).toInt
for (arg <- args.drop(1))
LongLines.processFile(arg, width)
}
}
I have first compiled LongLines.scala with the below command
[root@vm test_longlines]# scalac LongLines.scala
[root@vm test_longlines]# ls
FindLongLines.scala LongLines.class LongLines$.class LongLines.scala
Now, When I try to run with the below command I am getting cannot find symbol for 'LongLines'.
[root@vijayvm test_longlines]# scala -cp . FindLongLines.scala 10 LongLines.scala
/home/scala/dev/test_longlines/FindLongLines.scala:5: error: not found: value LongLines
LongLines.processFile(arg, width)
^
one error found
Should I gave to import LongLines in FindLongLines.scala ? Both are in the same folder but I have used package.
Upvotes: 3
Views: 8140
Reputation: 7845
1st, you didn't compile FindLongLines.scala
also?
2nd, in Scala you may have more than one object/class in a single File. Maybe it's overkill to separate so small objects in two files for a command line application
Upvotes: 2
Reputation: 191
Add the import line to the top
import package_name._
or
import package_name.longlines
Upvotes: 2