0
votes

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.

1
Why not just run sbt console? Or, if you want to use those classes on other environments, try sbt package to generate a single JAR with all your classes, and then pass that JAR to the cp argument. - Luis Miguel Mejía Suárez
I tried the JAR method, navigating to MyProject/src/main/scala/mypackage and running sbt package. This was successful, and a .jar file appeared at MyProject/src/main/scala/mypackage/target/scala-2.12/classes/mypackage_2.12-0.1.0-SNAPSHOT.jar. However, when I run scala -cp pathtojar, neither import mypackage nor val temp = new ClassOne(1.0) work. Any thoughts? - zack
Why exactly do you need this? If you just want to have a REPL with your classes you can always use sbt console. If you want to distribute your classes to other users, there are other alternatives. - Luis Miguel Mejía Suárez
To answer your question - I'm writing a package while reading a Scala textbook to put the concepts to practice. It might be bad practice (please tell me if it is), but I was planning on playing with the package's classes as I create them in order to gauge user friendliness / feature completeness. I tried your suggestion of sbt console from within MyProject and it worked! Thanks so much for your advice. - zack
Zack, that is pretty cool. Yesterday I was already falling of sleep and I read your last message wrong. You said that sbt console worked and somehow I understood that it didn't work. I am glad it helped. I deleted my last comment (since it doesn't make sense anymore) I would suggest you do the same with your last comment since it is off-topic to the site right now. - Luis Miguel Mejía Suárez

1 Answers

0
votes

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

Don't include the package in classpath