【问题标题】:How to use :phase power command in REPL?如何在 REPL 中使用 :phase power 命令?
【发布时间】:2019-12-26 00:06:15
【问题描述】:

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

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

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

scala> :phase parser
Active phase is now: Parser

什么power命令受到了它的影响?如何调用这些受影响的电源命令?它与:settings -Xprint:parser 有何不同?

【问题讨论】:

    标签: scala read-eval-print-loop


    【解决方案1】:

    该命令使您的代码被评估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

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多