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

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

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

相关标签:
2条回答
  • 2021-02-03 21:51

    If you call scala, you will get whatever scala version is installed on the path of your operating system.

    If you call sbt console, you get the scala version configured in the sbt build (build.sbt) with all libraries that are used in the build already on the classpath.

    See this answer for details.

    0 讨论(0)
  • 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._
    <console>: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.

    0 讨论(0)
提交回复
热议问题