Scala error - error: not found: type
I am a Java developer trying to learn Class creation in Scala.
I create two separate scala files. One file contains a trait. The second file extends the trait and defines a method.
Cola.scala This contains the trait.
trait Cola
{
def fizz():开发者_如何学JAVA String
}
Pepsi.scala This has a class that tries to extend the trait.
class Pepsi extends Cola
{
override def fizz():String = "1 Sugar"
}
var p = new Pepsi
println( " p = " + p.fizz )
When I excecute "scala Pepsi.scala" I get the following error message:
error: not found: type Cola
class Pepsi extends Cola
Both files are in the same folder.
Please help how to resolve this? I am using Scala 2.8 I am not using an editor so that I will be forced from scratch. This is to improve my knowledge about computers.
Try compiling them both and running them like you would a java command:
scalac Cola.scala Pepsi.scala
scala -classpath . Pepsi
Scala doesn't require you to put all of your traits and classes into eponymous (that is, named the same as the class) files. You can simply put all of that in one file and it will work. Or you can specify both files on the command line: Scala will not automatically look for Cola
in Cola.scala
.
精彩评论