Scala Compiler2.13.2 < Back


# Packages

-  package root

  The Scala compiler and reflection APIs.

  The Scala compiler and reflection APIs.

  Definition Classes
    root
-  package scala
  Definition Classes
    root
-  package tools
  Definition Classes
    scala
-  package nsc
  Definition Classes
    tools
-  package interpreter
  Definition Classes
    nsc
-  package jline
  Definition Classes
    interpreter
-  package shell
  Definition Classes
    interpreter
- AbstractOrMissingHandler
- CompletionCandidate
- ExprTyper
- IMain
- ImportContextPreamble
- Imports
- InteractiveReader
- MemberHandlers
- NamedParam
- NamedParamClass
- NamedParamCreator
- Naming
- Phased
- Power
- PresentationCompilation
- PresentationCompilationResult
- Repl
- ReplCore
- ReplDir
- ReplGlobal
- ReplOutput
- ReplReporter
- ReplRequest
- ReplStrings
- ReplVals
- Results
- ScriptedInterpreter
- ScriptedRepl
- StdReplTags
- StdReplVals
- TokenData
- {{

c

scala.tools.nsc.interpreter

# IMain

###
Companion object IMain

#### class IMain extends Repl with Imports with PresentationCompilation with Closeable

An interpreter for Scala code.

The main public entry points are compile(), interpret(), and bind(). The compile() method loads a complete Scala file. The interpret() method executes one line of Scala code at the request of the user. The bind() method binds an object to a variable that can then be used by later interpreted code.

The overall approach is based on compiling the requested code and then using a Java classloader and Java reflection to run the code and access its results.

In more detail, a single compiler instance is used to accumulate all successfully compiled or interpreted Scala code. To "interpret" a line of code, the compiler generates a fresh object that includes the line of code and which has public member(s) to export all variables defined by that code. To extract the result of an interpreted line to show the user, a second "result object" is created which imports the variables exported by the above object and then exports members called "$eval" and "$print". To accommodate user expressions that read from variables or methods defined in previous statements, "import" statements are used.

This interpreter shares the strengths and weaknesses of using the full compiler-to-Java. The main strength is that interpreted code behaves exactly as does compiled code, including running at full speed. The main weakness is that redefining classes and methods is not handled properly, because rebinding at the Java level is technically difficult.

Source
  IMain.scala

Linear Supertypes
Closeable, AutoCloseable, PresentationCompilation, Imports, Repl, ReplCore, AnyRef, Any
Known Subclasses
ScriptedInterpreter
Type Hierarchy

Ordering

1. Alphabetic
2. By Inheritance

Inherited

1. IMain
2. Closeable
3. AutoCloseable
4. PresentationCompilation
5. Imports
6. Repl
7. ReplCore
8. AnyRef
9. Any

Implicitly

1. by any2stringadd
2. by StringFormat
3. by Ensuring
4. by ArrowAssoc

1. Hide All
2. Show All

Visibility

1. Public
2. Protected

### Instance Constructors

1.  new IMain(interpreterSettings: Settings, reporter: ReplReporter)
2.  new IMain(settings: Settings, parentClassLoaderOverride: Option[ClassLoader], compilerSettings: Settings, reporter: ReplReporter)

### Type Members

1.  abstract class PhaseDependentOps extends AnyRef
2.  class ReadEvalPrint extends AnyRef

   Here is where we:

   Here is where we:

   1) Read some source code, and put it in the "read" object. 2) Evaluate the read object, and put the result in the "eval" object. 3) Create a String for human consumption, and put it in the "print" object.

   Read! Eval! Print! Some of that not yet centralized here.

3.  implicit class ReplTypeOps extends AnyRef
4.  class Request extends ReplRequest

   One line of code submitted by the user for interpretation

5.  type Setting = Settings.Setting
   Definition Classes
     Repl
6.  case class ComputedImports(header: String, prepend: String, append: String, access: String) extends Product with Serializable

   Compute imports that allow definitions from previous requests to be visible in a new request.

   Compute imports that allow definitions from previous requests to be visible in a new request. Returns three or four pieces of related code:

   0. Header code fragment that should go at the beginning of the compilation unit, specifically, import Predef.

   1. An initial code fragment that should go before the code of the new request.

   2. A code fragment that should go after the code of the new request.

   3. An access path which can be traversed to access any bindings inside code wrapped by #1 and #2 .

   The argument is a set of Names that need to be imported.

   Limitations: This method is not as precise as it could be. (1) It does not process wildcard imports to see what exactly they import. (2) If it imports any names from a request, it imports all of them, which is not really necessary. (3) It imports multiple same-named implicits, but only the last one imported is actually usable.

   Definition Classes
     Imports
7.  abstract class PresentationCompileResult extends PresentationCompilationResult
   Definition Classes
     PresentationCompilation

### Value Members

1.  final def !=(arg0: Any): Boolean
   Definition Classes
     AnyRef → Any
2.  final def ##: Int
   Definition Classes
     AnyRef → Any
3.  def +(other: String): String
   Implicit
     This member is added by an implicit conversion from IMain toany2stringadd[IMain] performed by method any2stringadd in scala.Predef.
   Definition Classes
     any2stringadd
4.  def ->[B](y: B): (IMain, B)
   Implicit
     This member is added by an implicit conversion from IMain toArrowAssoc[IMain] performed by method ArrowAssoc in scala.Predef.
   Definition Classes
     ArrowAssoc
   Annotations
     @inline()
5.  final def ==(arg0: Any): Boolean
   Definition Classes
     AnyRef → Any
6.  def addUrlsToClassPath(urls: URL*): Unit

   Adds all specified jars to the compile and runtime classpaths.

   Adds all specified jars to the compile and runtime classpaths.

   urls

     The list of items to add to the compile and runtime classpaths.

   Definition Classes
     IMain → Repl
   Note

     Currently only supports jars, not directories.

7.  def allDefinedNames: List[Global.Name]
8.  def allHandlers: collection.immutable.List[(memberHandlers)#MemberHandler]
9.  final def asInstanceOf[T0]: T0
   Definition Classes
     Any
10.  def backticked(s: String): String
11.  final def beQuietDuring(body: => Unit): Unit

    The reporter will not print results during execution of `body`.

    The reporter will not print results during execution of `body`.

    Definition Classes
      Repl → ReplCore
12.  def bind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
13.  def bind(p: NamedParam): Result
    Definition Classes
      IMain → Repl
14.  def bind(name: String, boundType: String, value: Any, modifiers: List[String] = Nil): Result

    Bind a specified name to a specified value.

    Bind a specified name to a specified value. The name may later be used by expressions passed to interpret.

    A fresh `ReadEvalPrint`, which defines a `line` package, is used to compile a custom `eval` object that wraps the bound value.

    If the bound value is successfully installed, then bind the name by interpreting `val name = $line42.$eval.value`.

    name

      the variable name to bind

    boundType

      the type of the variable, as a string

    value

      the object value to bind to it

    returns

      an indication of whether the binding succeeded

    Definition Classes
      IMain → ReplCore
15.  def bindValue(name: String, value: Any): Result

    Bind a specified `name` to a specified `value`.

    Bind a specified `name` to a specified `value`. The type is derived from the run-time class of the value.

    Definition Classes
      ReplCore
16.  def classLoader: AbstractFileClassLoader
    Definition Classes
      IMain → Repl
17.  def classOfTerm(id: String): Option[Class[_]]
18.  def classPathString: String
    Definition Classes
      IMain → Repl
19.  def cleanMemberDecl(owner: Global.Symbol, member: Global.Name): Global.Type
20.  def cleanTypeAfterTyper(sym: => Global.Symbol): Global.Type
21.  def clearExecutionWrapper(): Unit
    Definition Classes
      IMain → Repl
22.  def clone(): AnyRef
    Attributes
      protected[lang]
    Definition Classes
      AnyRef
    Annotations
      @throws(classOf[java.lang.CloneNotSupportedException]) @native()
23.  def close(): Unit

    This instance is no longer needed, so release any resources it is using.

    This instance is no longer needed, so release any resources it is using. The reporter's output gets flushed.

    Definition Classes
      IMain → Closeable → AutoCloseable → Repl
24.  def compile(line: String, synthetic: Boolean, fatally: Boolean): Either[Result, Request]
25.  def compile(line: String, synthetic: Boolean): Either[Result, Request]
26.  def compileSources(sources: SourceFile*): Boolean

    Compile an nsc SourceFile.

    Compile an nsc SourceFile. Returns true if there are no compilation errors, or false otherwise.

    Definition Classes
      IMain → Repl
27.  def compileSourcesKeepingRun(sources: SourceFile*): (Boolean, Run)
28.  def compileString(code: String): Boolean

    Compile a string.

    Compile a string. Returns true if there are no compilation errors, or false otherwise.

    Definition Classes
      IMain → Repl
29.  def compilerClasspath: Seq[URL]
    Definition Classes
      IMain → Repl
30.  def dealiasNonPublic(tp: Global.Type): Global.Type
31.  def debugging[T](msg: String)(res: T): T
32.  def definedSymbolList: collection.immutable.List[Global.Symbol]
33.  def definedTerms: collection.immutable.List[Global.TermName]
34.  def definedTypes: List[String]
    Definition Classes
      IMain → Repl
35.  def directBind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
36.  def directBind(p: NamedParam): Result
37.  def directBind(name: String, boundType: String, value: Any): Result
38.  final def ensureClassLoader(): Unit
39.  def ensuring(cond: (IMain) => Boolean, msg: => Any): IMain
    Implicit
      This member is added by an implicit conversion from IMain toEnsuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
40.  def ensuring(cond: (IMain) => Boolean): IMain
    Implicit
      This member is added by an implicit conversion from IMain toEnsuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
41.  def ensuring(cond: Boolean, msg: => Any): IMain
    Implicit
      This member is added by an implicit conversion from IMain toEnsuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
42.  def ensuring(cond: Boolean): IMain
    Implicit
      This member is added by an implicit conversion from IMain toEnsuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
43.  final def eq(arg0: AnyRef): Boolean
    Definition Classes
      AnyRef
44.  def equals(arg0: AnyRef): Boolean
    Definition Classes
      AnyRef → Any
45.  def executionWrapper: String
46.  def finalize(): Unit
    Definition Classes
      IMain → AnyRef
47.  def flatPath(sym: Global.Symbol): String
48.  def formatted(fmtstr: String): String
    Implicit
      This member is added by an implicit conversion from IMain toStringFormat[IMain] performed by method StringFormat in scala.Predef.
    Definition Classes
      StringFormat
    Annotations
      @inline()
49.  final def getClass(): Class[_ <: AnyRef]
    Definition Classes
      AnyRef → Any
    Annotations
      @native()
50.  def getClassIfDefined(path: String): Global.Symbol
51.  def getModuleIfDefined(path: String): Global.Symbol
52.  lazy val global: Global
53.  def hashCode(): Int
    Definition Classes
      AnyRef → Any
    Annotations
      @native()
54.  def implicitSymbolsBySource: List[(Global.Symbol, List[Global.Symbol])]
    Definition Classes
      Imports
55.  def implicitsCommandInternal(line: String): (List[String], String)
    Definition Classes
      IMain → Repl
56.  def importHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
57.  def importsCode(wanted: Set[Global.Name], request: Request, generousImports: Boolean): ComputedImports
    Attributes
      protected
    Definition Classes
      Imports
58.  def importsCommandInternal(tokens: List[String]): List[String]
    Definition Classes
      IMain → Repl
59.  def initializeCompiler(): Boolean
    Definition Classes
      IMain → Repl
60.  def initializeComplete: Boolean
    Definition Classes
      IMain → Repl
61.  def interpret(line: String, synthetic: Boolean): Result
    Definition Classes
      IMain → Repl
62.  def interpret(line: String): Result

    Interpret one line of input.

    Interpret one line of input. All feedback, including parse errors and evaluation results, are printed via the supplied compiler's reporter. Values defined are available for future interpreted strings.

    The return value is whether the line was interpreter successfully, e.g. that there were no parse errors.

    Definition Classes
      IMain → ReplCore
63.  def interpretFinally(line: String): Result

    Interpret one line of input.

    Interpret one line of input. All feedback, including parse errors and evaluation results, are printed via the supplied compiler's reporter. Values defined are available for future interpreted strings.

    The return value is whether the line was interpreter successfully, e.g. that there were no parse errors.

    Definition Classes
      IMain → Repl
64.  def interpretSynthetic(line: String): Result
65.  lazy val isClassBased: Boolean
66.  final def isInstanceOf[T0]: Boolean
    Definition Classes
      Any
67.  def isPackaged(line: String): Boolean

    Does code start with a package definition?

    Does code start with a package definition?

    Definition Classes
      IMain → Repl
68.  def kindCommandInternal(typeString: String, verbose: Boolean): String
    Definition Classes
      IMain → Repl
69.  def languageWildcardHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
    Definition Classes
      Imports
70.  def lastRequest: Request
71.  def lastWarnings: List[(Global.Position, String)]
    Definition Classes
      IMain → Repl
72.  lazy val memberHandlers: MemberHandlers { val intp: IMain.this.type }
73.  def mostRecentVar: String

    Returns the name of the most recent interpreter result.

    Returns the name of the most recent interpreter result. Mostly this exists so you can conveniently invoke methods on the previous result.

    Definition Classes
      IMain → Repl
74.  def nameToCode(s: String): String
75.  def namedDefinedTerms: List[String]
    Definition Classes
      IMain → Repl
76.  def namedParam[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): NamedParam
    Definition Classes
      IMain → Repl
77.  final def ne(arg0: AnyRef): Boolean
    Definition Classes
      AnyRef
78.  def newPresentationCompiler(): interactive.Global

    Create an instance of the presentation compiler with a classpath comprising the REPL's configured classpath and the classes output by previously compiled REPL lines.

    Create an instance of the presentation compiler with a classpath comprising the REPL's configured classpath and the classes output by previously compiled REPL lines.

    You may directly interact with this compiler from any thread, although you must not access it concurrently from multiple threads.

    You may downcast the `reporter` to `StoreReporter` to access type errors.

    Definition Classes
      PresentationCompilation
79.  final def notify(): Unit
    Definition Classes
      AnyRef
    Annotations
      @native()
80.  final def notifyAll(): Unit
    Definition Classes
      AnyRef
    Annotations
      @native()
81.  def onlyTerms(xs: List[Global.Name]): List[Global.TermName]
    Attributes
      protected
82.  def onlyTypes(xs: List[Global.Name]): List[Global.TypeName]
    Attributes
      protected
83.  def originalPath(sym: Global.Symbol): String
84.  def originalPath(name: Global.Name): String
85.  def originalPath(name: String): String
    Definition Classes
      IMain → Repl
86.  def outputDir: ReplDir
    Definition Classes
      IMain → Repl
87.  def parentClassLoader: ClassLoader

    Parent classloader.

    Parent classloader. Overridable.

    Attributes
      protected
88.  def parse(line: String, fatally: Boolean): Either[Result, (List[Global.Tree], Global.Position)]
89.  def parse(line: String): Either[Result, (List[Global.Tree], Global.Position)]

    Parse a line into and return parsing result (error, incomplete or success with list of trees)

90.  def parseString(line: String): Result

    TODO resolve scan, parse, compile, interpret, which just indicate how much work to do.

    TODO resolve scan, parse, compile, interpret, which just indicate how much work to do.

    Definition Classes
      IMain → Repl
91.  lazy val power: Power[StdReplVals]
    Definition Classes
      IMain → Repl
92.  def presentationCompile(cursor: Int, buf: String): Either[Result, PresentationCompilationResult]

    Typecheck a line of REPL input, suitably wrapped with "interpreter wrapper" objects/classes, with the presentation compiler.

    Typecheck a line of REPL input, suitably wrapped with "interpreter wrapper" objects/classes, with the presentation compiler. The result of this method gives access to the typechecked tree and to autocompletion suggestions.

    The caller is responsible for calling PresentationCompileResult#cleanup to dispose of the compiler instance.

    Definition Classes
      PresentationCompilation
93.  def prevRequestList: collection.immutable.List[Request]
94.  def quietBind(p: NamedParam): Result
    Definition Classes
      IMain → Repl
95.  def quietRun(code: String): Result
    Definition Classes
      IMain → Repl
96.  val readInstanceName: String
97.  def readRootPath(readPath: String): Global.Symbol
98.  def rebind(p: NamedParam): Result
99.  def recordRequest(req: Request): Unit
100.  def replClass: Class[_]
     Attributes
       protected
101.  def replScope: Global.Scope
102.  def replStrings: ReplStrings
103.  val reporter: ReplReporter
     Definition Classes
       IMain → Repl
104.  def requestDefining(name: String): Option[Request]
     Definition Classes
       IMain → Repl
105.  def reset(): Unit

     Reset this interpreter, forgetting all user-specified requests.

     Reset this interpreter, forgetting all user-specified requests.

     Definition Classes
       IMain → Repl
106.  def resetClassLoader(): Unit
107.  def runtimeClassAndTypeOfTerm(id: String): Option[(Class[_], Global.Type)]
108.  def runtimeMirror: Mirror
109.  def runtimeTypeOfTerm(id: String): Global.Type
110.  def setExecutionWrapper(code: String): Unit
     Definition Classes
       IMain → Repl
111.  val settings: Settings
     Definition Classes
       IMain → Repl
112.  def showDirectory: String
     Definition Classes
       IMain → Repl
113.  def symbolDefString(sym: Global.Symbol): String
114.  def symbolOfIdent(id: String): Global.Symbol
115.  def symbolOfLine(code: String): Global.Symbol
116.  def symbolOfName(id: Global.Name): Global.Symbol
117.  def symbolOfTerm(id: String): Global.Symbol
118.  def symbolOfType(id: String): Global.Symbol
119.  final def synchronized[T0](arg0: => T0): T0
     Definition Classes
       AnyRef
120.  def toString(): String
     Definition Classes
       AnyRef → Any
121.  def tokenize(line: String): List[TokenData]
     Definition Classes
       IMain → Repl
122.  def translateEnclosingClass(n: String): Option[String]
     Definition Classes
       IMain → Repl
123.  def translateOriginalPath(p: String): String
124.  def translatePath(path: String): Option[String]
     Definition Classes
       IMain → Repl
125.  def translateSimpleResource(path: String): Option[String]

     If path represents a class resource in the default package, see if the corresponding symbol has a class file that is a REPL artifact residing at a different resource path.

     If path represents a class resource in the default package, see if the corresponding symbol has a class file that is a REPL artifact residing at a different resource path. Translate X.class to $line3/$read$$iw$$iw$X.class.

126.  def tryTwice(op: => Global.Symbol): Global.Symbol

     It's a bit of a shotgun approach, but for now we will gain in robustness.

     It's a bit of a shotgun approach, but for now we will gain in robustness. Try a symbol-producing operation at phase typer, and if that is NoSymbol, try again at phase flatten. I'll be able to lose this and run only from exitingTyper as soon as I figure out exactly where a flat name is sneaking in when calculating imports.

127.  def typeCommandInternal(expr: String, verbose: Boolean): (String, String)

     TODO - -n normalize -l label with case class parameter names -c complete - leave nothing out

     TODO - -n normalize -l label with case class parameter names -c complete - leave nothing out

     Definition Classes
       IMain → Repl
128.  def typeOfExpression(expr: String, silent: Boolean = true): Global.Type
129.  def typeOfTerm(id: String): Global.Type
130.  def typeToCode(s: String): String
131.  def unqualifiedIds: List[String]
132.  def updateSettings(arguments: List[String]): Boolean
     Definition Classes
       IMain → Repl
133.  def userSetSettings: List[Setting]
     Definition Classes
       IMain → Repl
134.  def valueOfTerm(id: String): Option[Any]
     Definition Classes
       IMain → Repl
135.  def visibleSettings: List[Setting]
     Definition Classes
       IMain → Repl
136.  final def wait(): Unit
     Definition Classes
       AnyRef
     Annotations
       @throws(classOf[java.lang.InterruptedException])
137.  final def wait(arg0: Long, arg1: Int): Unit
     Definition Classes
       AnyRef
     Annotations
       @throws(classOf[java.lang.InterruptedException])
138.  final def wait(arg0: Long): Unit
     Definition Classes
       AnyRef
     Annotations
       @throws(classOf[java.lang.InterruptedException]) @native()
139.  def withLabel[A](temp: String)(body: => A): A
     Definition Classes
       IMain → Repl
140.  def withSuppressedSettings(body: => Unit): Unit
     Definition Classes
       IMain → Repl
141.  def withoutWarnings[T](body: => T): T
142.  object deconstruct extends StructuredTypeStrings
143.  object exprTyper extends ExprTyper
144.  object flatOp extends PhaseDependentOps
145.  object naming extends Naming
146.  object replOutput extends ReplOutput
147.  object typerOp extends PhaseDependentOps

### Deprecated Value Members

1.  final def setContextClassLoader(): Unit
   Definition Classes
     ReplCore
   Annotations
     @deprecated
   Deprecated

     (Since version 2.12.0) The thread context classloader is now set and restored around execution of REPL line, this method is now a no-op.

2.  def →[B](y: B): (IMain, B)
   Implicit
     This member is added by an implicit conversion from IMain toArrowAssoc[IMain] performed by method ArrowAssoc in scala.Predef.
   Definition Classes
     ArrowAssoc
   Annotations
     @deprecated
   Deprecated

     (Since version 2.13.0) Use `->` instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.

### Inherited from Closeable

### Inherited from AutoCloseable

### Inherited from PresentationCompilation

### Inherited from Imports

### Inherited from Repl

### Inherited from ReplCore

### Inherited from AnyRef

### Inherited from Any

### Inherited by implicit conversion any2stringadd fromIMain to any2stringadd[IMain]

### Inherited by implicit conversion StringFormat fromIMain to StringFormat[IMain]

### Inherited by implicit conversion Ensuring fromIMain to Ensuring[IMain]

### Inherited by implicit conversion ArrowAssoc fromIMain to ArrowAssoc[IMain]

### Ungrouped