zack
zack

Reputation: 460

SBT-compiled package not working properly in Scala REPL

I'm developing a Scala package using an sbt project scheme through IntelliJ. I wanted to use the classes in this package in the Scala REPL, so I followed the instructions in this post. Namely,

  1. Navigate to where my package's source files are located (MyProject/src/main/scala/mypackage)
  2. Run "sbt compile"
  3. Navigate to where sbt stores the compiled source files (MyProject/src/main/scala/mypackage/target/scala-2.12/classes/mytest)
  4. Run "scala -cp ."

This all works fine until I try to create an instance of one of my package's classes and I get the error MyClass does not have a constructor. However, if I just paste the class definitions into the Scala REPL, I can create instances with no errors. What am I doing wrong here?


As a reproducible example, I created a package mytest with two files TraitOne.scala and ClassOne.scala. The first file looks like

package mytest

trait TraitOne {
  val a: Double
  def aMinus(x: Double): Double = a - x
}

The second file looks like

package mytest

class ClassOne(val a: Double) extends TraitOne {
  def aPlus(x: Double): Double = a + x 
}

If I follow steps 1 - 4 as listed above and write val temp = new ClassOne(1.0), I get the error ClassOne does not have a constructor. But if I paste

trait TraitOne {
  val a: Double
  def aMinus(x: Double): Double = a - x
}
class ClassOne(val a: Double) extends TraitOne {
  def aPlus(x: Double): Double = a + x 
}

into the REPL, val temp = new ClassOne(1.0) works fine.

Upvotes: 0

Views: 61

Answers (1)

joeljohansson
joeljohansson

Reputation: 11

Run scala -cp MyProject/src/main/scala/mypackage/target/scala-2.12/classes

Don't include the package in classpath

Upvotes: 0

Related Questions