Răzvan Flavius Panda
Răzvan Flavius Panda

Reputation: 22116

How to run scala code on scala console start?

Is it possible to run some scala code each time the scala console repl starts (this code has to change the REPL context)?

Was expecting there might be some .scala or similar config file which would allow setting that.

My use case is running this code on each REPL start: https://stackoverflow.com/a/6770870/750216

Upvotes: 1

Views: 213

Answers (2)

Răzvan Flavius Panda
Răzvan Flavius Panda

Reputation: 22116

When running with sbt console:

I created a %USERPROFILE%\.sbt\0.13\global.sbt global configuration file and added the start code to it:

initialCommands in console := """
    def viewdoc[A](a: A) {
    val name = a.asInstanceOf[AnyRef].getClass.getName
    val url = "http://www.scala-lang.org/api/current/index.html#"+name
    val pb = new ProcessBuilder("firefox",url)
    val p = pb.start
    p.waitFor
    }
"""

Running sbt console:

C:\WINDOWS>sbt console
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Set current project to windows (in build file:/C:/Windows/)
[info] Starting scala interpreter...
[info]
viewdoc: [A](a: A)Unit
Welcome to Scala version 2.10.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_74).
Type in expressions to have them evaluated.
Type :help for more information.

When running with scala:

See question here: Call another program with arguments passed to the currently executing batch file

Upvotes: 1

Reactormonk
Reactormonk

Reputation: 21700

name := "scala-playground"
version := "0.1-SNAPSHOT"
organization := "org.reactormonk"
scalaVersion := "2.11.7"

resolvers ++= Seq(
  "ScalaNLP Maven2" at "http://repo.scalanlp.org/repo",
  "Scala Tools Snapshots" at "http://scala-tools.org/repo-snapshots/",
  "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
)

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "2.2.1",
  "io.argonaut" %% "argonaut" % "6.1",
  "com.github.alexarchambault" %% "argonaut-shapeless_6.1" % "1.0.0-M1",
  "com.github.pathikrit"  %% "better-files-akka"  % "2.15.0"
)

javaOptions += "-Xmx4g"
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.7.1")
initialCommands in console := """
import scalaz._, Scalaz._
//import shapeless._
"""

Upvotes: 1

Related Questions