Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

scala - SBT编译的软件包在Scala REPL中无法正常工作(SBT-compiled package not working properly in Scala REPL)

I'm developing a Scala package using an sbt project scheme through IntelliJ.

(我正在通过IntelliJ使用sbt项目方案开发Scala软件包。)

I wanted to use the classes in this package in the Scala REPL, so I followed the instructions in this post .

(我想在这个包在斯卡拉REPL使用类,所以我跟着说明这个职位 。)

Namely,

(即)

  1. Navigate to where my package's source files are located (MyProject/src/main/scala/mypackage)

    (导航到我的包的源文件所在的位置(MyProject / src / main / scala / mypackage))

  2. Run "sbt compile"

    (运行“ sbt编译”)

  3. Navigate to where sbt stores the compiled source files (MyProject/src/main/scala/mypackage/target/scala-2.12/classes/mytest)

    (导航到sbt存储已编译源文件的位置(MyProject / src / main / scala / mypackage / target / scala-2.12 / classes / mytest))

  4. Run "scala -cp ."

    (运行“ 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 .

(在我尝试创建包的一个类的实例并且收到错误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.

(但是,如果仅将类定义粘贴到Scala REPL中,则可以创建没有错误的实例。)

What am I doing wrong here?

(我在这里做错了什么?)


As a reproducible example, I created a package mytest with two files TraitOne.scala and ClassOne.scala .

(作为可重现的示例,我使用两个文件TraitOne.scalaClassOne.scala创建了一个包mytest 。)

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 .

(如果我按照上面列出的步骤1-4编写val temp = new ClassOne(1.0)ClassOne does not have a constructor收到错误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.

(在REPL中, val temp = new ClassOne(1.0)可以正常工作。)

  ask by zack translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(运行scala -cp MyProject/src/main/scala/mypackage/target/scala-2.12/classes)

Don't include the package in classpath

(不要将包包含在类路径中)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...