Reputation: 381
I am starting out in Scala with SBT, making a Hello World program.
Here's my project layout:
I've made sure to download the very latest JDK and Scala, and configure my Project Settings. Here's my build.sbt
:
name := "Coursera_Scala"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
Hello.scala
itself compiles okay:
package demo
class Hello {
def sayHelloTo(name: String) = "Hello, $name!"
}
However, my accompanying HelloTest.scala
does not. Here's the test:
package demo
import org.scalatest.FunSuite
class HelloTest extends FunSuite {
test("testSayHello") {
val result = new Hello.sayHelloTo("Scala")
assert(result == "Hello, Scala!")
}
}
Here's the error:
Error:(8, 22) not found: value Hello
val result = new Hello.sayHello("Scala")
^
In addition to the compile error, Intellij shows errors "Cannot resolve symbol" for the symbols Hello
, assert
, and ==
. This leads me to believe that the build is set up incorrectly, but then wouldn't there be an error on the import?
Upvotes: 2
Views: 1466
Reputation: 729
Or you can use new Hello().sayHelloTo(...)
. Writing () should create a new instance and then call the method.
Upvotes: 1
Reputation: 14842
The problem is this expression:
new Hello.sayHelloTo("Scala")
This creates a new instance of the class sayHelloTo
defined on the value Hello
. However, there is no value Hello
, just the class Hello
.
You want to write this:
(new Hello).sayHelloTo("Scala")
This creates a new instance of the class Hello
and calls sayHelloTo
on the instance.
Upvotes: 3