package and import
1 | ├── build.sbt |
对于同一目录下的两个文件,可以不用import,默认在一个package内
1 | //Interleave.scala |
1 | //utils.scala |
Interleave.scala 不需要import ClassInUtils ,直接使用就可以,sbt 会帮你搞定编译
sbt import 错误解决
1 | scalaDSL> tree src ~/lab/scalaDSL |
1 | import scala.util.parsing.combinator._ |
sbt run
总是会显示1
2
3.....object parsing is not a member of pacakge util
[error] import scala.util.parsing.combinator._
[error] ^
scala src/main/scala/parser.scala
可以正常打印Hello scala ***
命令行的scala版本2.12.6, build.sbt 还是scala 2.12.6 ,所以怀疑是不是sbt的版本有问题
查看project/build.property
版本号为1.2.8 ,改为1.1.1, 1.1.2都是同样的问题
后来在网上搜到,build.sbt需要添加librarydependence 。如下1
2
3
4
5name := "scalaDSL"
version := "0.1"
//添加下面一行
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.2"
scalaVersion := "2.11.12"
重新sbt run
编译正常
备注:由于在网上搜到的scala-parser-combinators版本为1.0.2,对应的scala版本是2.11
因此 scalaVersion := "2.12.6"
改成 scalaVersion := "2.11.12"
同理IDEA设置中有时候发现scala sbt版本都设置正确了,但是有些库还是找不到,显示红色,
很可能就是 libraryDependencies 设置问题
其他问题
在SCALA中import scala.swing._
直接编译并不报错
但是以下程序编译会报错 [error] object swing is not a member of package scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//main.c
import scala.swing._
class UI2 extends MainFrame {
title = "GUI Program #3"
contents = new BoxPanel(Orientation.Vertical){
contents += new Label("Look at me!")
}
}
object GuiProgramTwo {
def main(args: Array[String]) {
val ui = new UI2
ui.visible = true
}
}
原因: As of 2.11, the scala swing package is no longer listed among scala’s standard library api and in fact is described in its own README as “mostly unsupported”.
因此需要安装最新的scala-swing版本,需要在build.sbt添加libraryDependencies += "org.scala-lang" % "scala-swing" % "2.10.4"
scala-swing最新版本地址scala-swing
编译成功运行
Programming in Scala 3rd Edition上的示例并不能成功运行,
这篇scala-swing GUI教程上的例子能正常运行