加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何在REPL中检查隐式/丰富的转换和实现的特征

发布时间:2020-12-16 19:09:53 所属栏目:安全 来源:网络整理
导读:Scala中的某些东西对我来说似乎是不透明的,如下所示的不是Int的成员函数: 1.to(4) 我可以在没有咨询语言参考的情况下检查什么行为导致(隐性转换或特征或其他)?那也在REPL? 如果REPL不能帮助,有没有一些友好的选择? 解决方法 与Scala 2.9: ~/code/scala
Scala中的某些东西对我来说似乎是不透明的,如下所示的不是Int的成员函数:

1.to(4)

我可以在没有咨询语言参考的情况下检查什么行为导致(隐性转换或特征或其他)?那也在REPL?

如果REPL不能帮助,有没有一些友好的选择?

解决方法

与Scala 2.9:

~/code/scala scala -Xprint:typer -e "1 to 4"
[[syntax trees at end of typer]]// Scala source: scalacmd4469348504265784881.scala
package <empty> {
  final object Main extends java.lang.Object with ScalaObject {
    def this(): object Main = {
      Main.super.this();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def this(): anonymous class $anon = {
            $anon.super.this();
            ()
          };
          scala.this.Predef.intWrapper(1).to(4)
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}

与Scala 2.10或2.11:

scala> import reflect.runtime.universe
import reflect.runtime.universe

scala> val tree = universe.reify(1 to 4).tree
tree: reflect.runtime.universe.Tree = Predef.intWrapper(1).to(4)

scala> universe.showRaw(tree)
res0: String = Apply(Select(Apply(Select(Ident(scala.Predef),newTermName("intWrapper")),List(Literal(Constant(1)))),newTermName("to")),List(Literal(Constant(4))))

scala> universe.show(tree)
res1: String = Predef.intWrapper(1).to(4)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读