如何在REPL中使用:phase power命令?

时间:2019-12-26 00:06:15

标签: scala read-eval-print-loop

Power mode of Scala REPL的启用:phase命令:

:phase <phase>           set the implicit phase for power commands

我似乎找不到有关如何使用此命令的文档。例如,假设我设置了parser编译器阶段

scala> :phase parser
Active phase is now: Parser

哪些 power 命令已受到影响?如何调用这些受影响的电源命令?与:settings -Xprint:parser有什么区别?

1 个答案:

答案 0 :(得分:3)

该命令使您的代码被评估为enteringPhase,这是通过使用intp.setExecutionWrapper完成的。您可以使用-Dscala.repl.debug见证包装。

它包装了REPL的“印刷品”:

lazy val $print: _root_.java.lang.String = $line4.$read.$iw.$iw.$r.phased.atCurrent {

phased对象仅使调用适应enteringPhase

在下面的示例中,您稍后会看到擦除的类型:

scala> global.rootMirror.staticClass("scala.Option").typeSignature
res0: $r.global.Type =
[+A <: <?>]AnyRef
        with IterableOnce[A]
        with Product
        with Serializable {
  def <init>(): Option[A]
  final def isEmpty: Boolean
  final def isDefined: Boolean
  final override def knownSize: Int
  def get: A
  final def getOrElse[B >: A](default: => B): B
  final def orNull[A1 >: A](implicit ev: Null <:< A1): A1
  final def map[B](f: A => B): Option[B]
  final def fold[B](ifEmpty: => B)(f: A => B): B
  final def flatMap[B](f: A => Option[B]): Option[B]
  def flatten[B](implicit ev: A <:< Option[B]): Option[B]
  final def filter(p: A => Boolean): Option[A]
  final def filterNot(p: A => Boolean): Option[A]
  final def nonEmpty: Boolean
  final def withFilter(p: A => Boolean): Option.this.WithFilter
  class WithFilter extends AnyRef
  final def...

scala> global.rootMirror.staticClass("scala.Option").typeSignature.getClass
res1: Class[_ <: $r.global.Type] = class scala.reflect.internal.Types$PolyType

scala> :phase cleanup
Active phase is now: Cleanup

scala> global.rootMirror.staticClass("scala.Option").typeSignature.getClass
res2: Class[_ <: $r.global.Type] = class scala.reflect.internal.Types$ClassInfoType

scala> global.rootMirror.staticClass("scala.Option").typeSignature
res3: $r.global.Type =
Object
        with scala.collection.IterableOnce
        with Product
        with java.io.Serializable {
  def <init>(): Option
  final def isEmpty(): Boolean
  final def isDefined(): Boolean
  final override def knownSize(): Int
  def get(): Object
[snip]

REPL中的:type命令不会受到影响,因为它调用了exitingTyper