Scala中的模式匹配如何在字节码级别实现? [英] How is pattern matching in Scala implemented at the bytecode level?

查看:75
本文介绍了Scala中的模式匹配如何在字节码级别实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala中的模式匹配如何在字节码级别实现?

How is pattern matching in Scala implemented at the bytecode level?

就像一系列的 if(x Foo实例)

Is it like a series of if (x instanceof Foo) constructs, or something else? What are its performance implications?

例如,给出以下代码(来自按比例缩放 第46-48页), eval 方法的等效Java代码将如何显示?

For example, given the following code (from Scala By Example pages 46-48), how would the equivalent Java code for the eval method look like?

abstract class Expr
case class Number(n: Int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr

def eval(e: Expr): Int = e match {
  case Number(x) => x
  case Sum(l, r) => eval(l) + eval(r)
}

我可以读取Java字节码,所以字节码表示形式对我来说已经足够好了,但是其他读者可能更清楚知道它看起来像Java代码。

P.S. I can read Java bytecode, so a bytecode representation would be good enough for me, but probably it would be better for the other readers to know how it would look like as Java code.

PPS这本书在Scala中编程给出了答案吗?以及有关如何实施Scala的类似问题?我已经订购了这本书,但还没到。

P.P.S. Does the book Programming in Scala give an answer to this and similar questions about how Scala is implemented? I have ordered the book, but it has not yet arrived.

推荐答案

可以通过反汇编程序来探索低层次的内容,但是简短答案是,一堆if / elses谓词取决于模式

The low level can be explored with a disassembler but the short answer is that it's a bunch of if/elses where the predicate depends on the pattern

case Sum(l,r) // instance of check followed by fetching the two arguments and assigning to two variables l and r but see below about custom extractors 
case "hello" // equality check
case _ : Foo // instance of check
case x => // assignment to a fresh variable
case _ => // do nothing, this is the tail else on the if/else

的尾巴处理类似模式或模式和组合(例如 case Foo(45,x)),但通常这些只是我刚刚描述的逻辑扩展。模式也可以具有防护,这是对谓词的附加约束。在某些情况下,编译器可以优化模式匹配,例如,在两种情况之间有一些重叠时,编译器可能会合并一些内容。高级模式和优化是编译器中的工作重点,因此,如果字节码比当前和将来版本的Scala的这些基本规则有了实质性的改进,请不要感到惊讶。

There's much more that you can do with patterns like or patterns and combinations like "case Foo(45, x)", but generally those are just logical extensions of what I just described. Patterns can also have guards, which are additional constraints on the predicates. There are also cases where the compiler can optimize pattern matching, e.g when there's some overlap between cases it might coalesce things a bit. Advanced patterns and optimization are an active area of work in the compiler, so don't be surprised if the byte code improves substantially over these basic rules in current and future versions of Scala.

除此之外,除了Scala用于案例类的默认提取器之外,您还可以编写自己的自定义提取器。如果您这样做,那么模式匹配的成本就是提取程序所做的任何事情的成本。在 http://lamp.epfl.ch/~emir中可以找到很好的概述。 /write/MatchingObjectsWithPatterns-TR.pdf

In addition to all that, you can write your own custom extractors in addition to or instead of the default ones Scala uses for case classes. If you do, then the cost of the pattern match is the cost of whatever the extractor does. A good overview is found in http://lamp.epfl.ch/~emir/written/MatchingObjectsWithPatterns-TR.pdf

这篇关于Scala中的模式匹配如何在字节码级别实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆