如何在Scala 2.10中实现延迟val类变量?
发布时间:2020-12-16 09:35:21 所属栏目:安全 来源:网络整理
导读:This answer至 What’s the (hidden) cost of Scala’s lazy val?显示了它们在Scala 2.7中的实现。但是正如评论所说,这一定从此改变了,所以我很好奇,现在(2.10)类的懒惰变量的实现是什么? 解决方法 用scala 2.10.2编译: class Foo { lazy val bar = mat
This answer至
What’s the (hidden) cost of Scala’s lazy val?显示了它们在Scala 2.7中的实现。但是正如评论所说,这一定从此改变了,所以我很好奇,现在(2.10)类的懒惰变量的实现是什么?
解决方法
用scala 2.10.2编译:
class Foo { lazy val bar = math.pow(5,3) } 然后用JD-GUI反编译结果: import scala.math.package.; import scala.reflect.ScalaSignature; @ScalaSignature(bytes=" 06 01e1A! 01 02 01 13t 31ai8 13 03rtq 01P3naRLhh 01 01 24 05 011 01CA 04 13 33 05A!"A 05 02 13M 34 27r1n 05-A!AB!osJ+grC 03 16 01 21 05a" 01 04=S:LGO 20 13 02 37A 21 01 03A 07 02 05!A! 03 01EC 02 23 051#A 02cCJ, 22 01 06t 03 17UI!A 06 05 03r 21{WO 317f 21!A 02 01#A!B 23! 22 01 022be 02 02") public class Foo { private double bar; private volatile boolean bitmap$0; private double bar$lzycompute() { synchronized (this) { if (!this.bitmap$0) { this.bar = package..MODULE$.pow(5.0D,3.0D); this.bitmap$0 = true; } return this.bar; } } public double bar() { return this.bitmap$0 ? this.bar : bar$lzycompute(); } } 编辑 – 这是三个字段的样子: class Foo { lazy val a = math.pow(5,1) lazy val b = math.pow(5,2) lazy val c = math.pow(5,3) } 反编译: import scala.math.package.; import scala.reflect.ScalaSignature; @ScalaSignature(bytes=" 06 01 052A! 01 02 01 13t 31ai8 13 03rtq 01P3naRLhh 01 01 24 05 011 01CA 04 13 33 05A!"A 05 02 13M 34 27r1n 05-A!AB!osJ+grC 03 16 01 21 05a" 01 04=S:LGO 20 13 02 37A 21 01 03A 07 02 05!A! 03 01EC 02 23 051#A 01b+ 05! 02CA 04 26 23t1 02B 01 04E_V 24G. 32 05t1 01At 21)Q 05) 05 21 21rt 05t5 01A) 31!C 01' 05t!r 03 05 35 01! 05t 25) 03 25 03t 21 07 05 03 05 37 01! 25r 21" 01 24 03 05 31 07 02 03 21 01 21 03 05 13 25 02 13 02 05r 04 03") public class Foo { private double a; private double b; private double c; private volatile byte bitmap$0; private double a$lzycompute() { synchronized (this) { if ((byte)(this.bitmap$0 & 0x1) == 0) { this.a = package..MODULE$.pow(5.0D,1.0D); this.bitmap$0 = ((byte)(this.bitmap$0 | 0x1)); } return this.a; } } private double b$lzycompute() { synchronized (this) { if ((byte)(this.bitmap$0 & 0x2) == 0) { this.b = package..MODULE$.pow(5.0D,2.0D); this.bitmap$0 = ((byte)(this.bitmap$0 | 0x2)); } return this.b; } } private double c$lzycompute() { synchronized (this) { if ((byte)(this.bitmap$0 & 0x4) == 0) { this.c = package..MODULE$.pow(5.0D,3.0D); this.bitmap$0 = ((byte)(this.bitmap$0 | 0x4)); } return this.c; } } public double a() { return (byte)(this.bitmap$0 & 0x1) == 0 ? a$lzycompute() : this.a; } public double b() { return (byte)(this.bitmap$0 & 0x2) == 0 ? b$lzycompute() : this.b; } public double c() { return (byte)(this.bitmap$0 & 0x4) == 0 ? c$lzycompute() : this.c; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容