What is the difference of running `scala` and `sbt console`?

后端 未结 2 1075
你的背包
你的背包 2021-02-03 21:25

What is the difference of running the scala shell in these different ways?

2条回答
  •  情歌与酒
    2021-02-03 22:05

    SBT is tied to a specific project defined by a build.sbt file in a way that $ sbt console will load up the same REPL environment as $ scala but with the addition of all of the project code and dependencies defined in the build available for import. Also, it will use the version of Scala defined by build.sbt.

    For example:

    $ scala
    scala> import scalaz._
    :7: error: not found: value scalaz
           import scalaz._
    

    but with this build.sbt:

    scalaVersion := "2.11.4"
    
    libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0"
    

    the import succeeds:

    $ sbt console
    ...
    scala> import scalaz._
    import scalaz._
    

    The command loads Scala 2.11.4 instead of the system wide Scala (or any version of it on PATH).

    Furthermore, invoking sbt console after adding new items in the build’s libraryDependencies will fetch them.

提交回复
热议问题