Scala reduceLeft 实现中的 0.asInstanceOf[B] 发生了什么 [英] What is happening with 0.asInstanceOf[B] in Scala reduceLeft implementation

查看:55
本文介绍了Scala reduceLeft 实现中的 0.asInstanceOf[B] 发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 Scala TraversableOnce 的 reduceLeft 方法的来源特征.读取 var acc: B = 0.asInstanceOf[B] 的行发生了什么?

Below is the source for the reduceLeft method of Scala's TraversableOnce trait. What is happening with the line that reads var acc: B = 0.asInstanceOf[B]?

对我来说,似乎如果我在字符串列表上调用它,例如 List("a", "b", "c"),这将导致类似 <代码>0.asInstanceOf[String].但是,如果我直接尝试,0.asInstanceOf[String] 在运行时会抛出一个 ClassCastException.

To me, it seems that if I call this on a list of Strings, such as List("a", "b", "c"), this would result in something like 0.asInstanceOf[String]. However, 0.asInstanceOf[String] throws a ClassCastException at run time if I try it directly.

那一行发生了什么,为什么它与在字符串列表上调用时直接调用 0.asInstanceOf[String] 不同?

What is happening with that line, and why is it different than calling 0.asInstanceOf[String] directly when called on a list of Strings?

def reduceLeft[B >: A](op: (B, A) => B): B = {
  if (isEmpty)
    throw new UnsupportedOperationException("empty.reduceLeft")

  var first = true
  var acc: B = 0.asInstanceOf[B]

  for (x <- self) {
    if (first) {
      acc = x
      first = false
    }
    else acc = op(acc, x)
  }
  acc
}

额外问题:为什么 acc 甚至初始化为该值?看起来 for 循环的第一次迭代中的代码只会用 TraversableOnce 对象中的第一个元素覆盖该值.

Bonus question: why is acc even initialized to that value? It looks like the code in the first iteration of the for loop will just overwrite that value with the first element in the TraversableOnce object.

推荐答案

嗯,在运行时不会因为 ClassCastException 而失败的原因是:

Well, the reason this is not failing with a ClassCastException at runtime is either because:

  1. 编译器删除了从未使用过的初始化推理(我对此表示怀疑)
  2. B 被擦除Object(或 AnyRef),因此转换实际上是 0.asInstanceOf[Object]
  1. The compiler removes the initialization reasoning that it is never used (I doubt this)
  2. B is erased to Object (or AnyRef), hence the cast is really 0.asInstanceOf[Object]

您可以通过检查字节码来测试第一种可能性,但这似乎是一段毫无意义的代码.如果它没有被省略,那么每次调用该方法时都会产生不必要的装箱 Int 开销(尽管不是创建对象 - 见下文)!

You could test the first possibility by checking the bytecode but it seems a pointless piece of code. If it is not elided, then this has the unnecessary overhead of boxing an Int on every call to the method (although not an object creation - see below)!

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait X[A] {
 | def foreach[U](f: A => U): U
 | def isEmpty = {
 | var b = false
 | foreach(_ => b = true)
 | !b
 | }
 | def reduceLeft[B >: A](op: (B, A) => B): B = {
 | if (isEmpty) sys.error("Bad")
 | var first = true
 | var acc: B = 0.asInstanceOf[B]
 | for (x <- this) {
 | if (first) { acc = x; first = false } else acc = op(acc, x)
 | }
 | acc
 | }
 | }
defined trait X

现在:

scala> :javap -v X
Compiled from "<console>"
public interface X extends scala.ScalaObject
  SourceFile: "<console>"
  Scala: length = 0x

  Signature: length = 0x2
   00 0D 
  InnerClass: 
   public abstract #19= #16 of #18; //X=class X of class 
   public final #21; //class X$$anonfun$isEmpty$1
   public final #23; //class X$$anonfun$reduceLeft$1
  minor version: 0
  major version: 49
  Constant pool:
const #1 = Asciz    SourceFile;
const #2 = Asciz    <console>;
const #3 = Asciz    foreach;
const #4 = Asciz    (Lscala/Function1;)Ljava/lang/Object;;
const #5 = Asciz    <U:Ljava/lang/Object;>(Lscala/Function1<TA;TU;>;)TU;;
const #6 = Asciz    Signature;
const #7 = Asciz    isEmpty;
const #8 = Asciz    ()Z;
const #9 = Asciz    reduceLeft;
const #10 = Asciz   (Lscala/Function2;)Ljava/lang/Object;;
const #11 = Asciz   <B:Ljava/lang/Object;>(Lscala/Function2<TB;TA;TB;>;)TB;;
const #12 = Asciz   Scala;
const #13 = Asciz   <A:Ljava/lang/Object;>Ljava/lang/Object;Lscala/ScalaObject;;
const #14 = Asciz   InnerClasses;
const #15 = Asciz   X;
const #16 = class   #15;    //  X
const #17 = Asciz   ;
const #18 = class   #17;    //  
const #19 = Asciz   X;
const #20 = Asciz   X$$anonfun$isEmpty$1;
const #21 = class   #20;    //  X$$anonfun$isEmpty$1
const #22 = Asciz   X$$anonfun$reduceLeft$1;
const #23 = class   #22;    //  X$$anonfun$reduceLeft$1
const #24 = Asciz   java/lang/Object;
const #25 = class   #24;    //  java/lang/Object
const #26 = Asciz   scala/ScalaObject;
const #27 = class   #26;    //  scala/ScalaObject

{
public abstract java.lang.Object foreach(scala.Function1);
  Signature: length = 0x2
   00 05 

public abstract boolean isEmpty();

public abstract java.lang.Object reduceLeft(scala.Function2);
  Signature: length = 0x2
   00 0B 

}

随心所欲!

另一种可能性是将其放在源文件中并编译它,打印出中间代码阶段:

One other possibility is to put this in a source file and compile it, printing out the intermediate code phase:

./scalac -Xprint:icode X.scala

然后你得到...

def reduceLeft($this: X, op$1: Function2): java.lang.Object = {
  if ($this.isEmpty())
    scala.sys.`package`.error("Bad")
  else
    ();
  var first$1: scala.runtime.BooleanRef = new scala.runtime.BooleanRef(true);
  var acc$1: scala.runtime.ObjectRef = new scala.runtime.ObjectRef(scala.Int.box(0));
  $this.foreach({
    (new anonymous class X$$anonfun$reduceLeft$1($this, op$1, first$1, acc$1): Function1)
  });
  acc.elem
};

很明显,那里有不必要的拳击!有一点是,这不会涉及对象创建,仅涉及查找,因为 -127 到 127 的装箱值已被缓存.

It looks distinctly like the unnecessary boxing is in there! One point is that this won't involve an object creation, merely a lookup, because the boxed values of -127 to 127 are cached.

您可以通过将上述打印命令中的编译器阶段更改为擦除"来检查已擦除的行.你好:

You can check the erased line by changing the compiler phase in the above print command to "erasure". Hey presto:

var acc: java.lang.Object = scala.Int.box(0).$asInstanceOf[java.lang.Object]();

这篇关于Scala reduceLeft 实现中的 0.asInstanceOf[B] 发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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