Last active
August 19, 2019 16:34
-
-
Save fthomas/0f914bf5c5abda57cce1 to your computer and use it in GitHub Desktop.
implicit value class vs. implicit def + value class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class A(i: Int) | |
object A { | |
implicit class ASyntax(val self: A) extends AnyVal { | |
def twice: Int = self.i * 2 | |
} | |
implicit def toASyntax2(a: A): ASyntax2 = | |
new ASyntax2(a) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ASyntax2(val self: A) extends AnyVal { | |
def once: Int = self.i | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class B { | |
val a = A(1) | |
a.once | |
a.twice | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Classfile /home/frank/syn/B.class | |
Last modified Mar 31, 2015; size 942 bytes | |
MD5 checksum 7d0367f3829360308b4da60e28045012 | |
Compiled from "B.scala" | |
public class B | |
SourceFile: "B.scala" | |
InnerClasses: | |
public static #57= #47 of #21; //ASyntax$=class A$ASyntax$ of class A | |
RuntimeVisibleAnnotations: | |
0: #6(#7=s#8) | |
ScalaSig: length = 0x3 | |
05 00 00 | |
minor version: 0 | |
major version: 50 | |
flags: ACC_PUBLIC, ACC_SUPER | |
Constant pool: | |
#1 = Utf8 B | |
#2 = Class #1 // B | |
#3 = Utf8 java/lang/Object | |
#4 = Class #3 // java/lang/Object | |
#5 = Utf8 B.scala | |
#6 = Utf8 Lscala/reflect/ScalaSignature; | |
#7 = Utf8 bytes | |
#8 = Utf8 e1A!\t\t!ICaT-9usza\rAqAC)\t\"AtGd-\t1I\=SKDQ!9\taP5oSRtD#AAQ\"\tI!!C'\t-F!\tR#\t\t\tIFC | |
#9 = Utf8 a | |
#10 = Utf8 LA; | |
#11 = Utf8 ()LA; | |
#12 = NameAndType #9:#10 // a:LA; | |
#13 = Fieldref #2.#12 // B.a:LA; | |
#14 = Utf8 this | |
#15 = Utf8 LB; | |
#16 = Utf8 <init> | |
#17 = Utf8 ()V | |
#18 = NameAndType #16:#17 // "<init>":()V | |
#19 = Methodref #4.#18 // java/lang/Object."<init>":()V | |
#20 = Utf8 A | |
#21 = Class #20 // A | |
#22 = Utf8 (I)V | |
#23 = NameAndType #16:#22 // "<init>":(I)V | |
#24 = Methodref #21.#23 // A."<init>":(I)V | |
#25 = Utf8 ASyntax2$ | |
#26 = Class #25 // ASyntax2$ | |
#27 = Utf8 MODULE$ | |
#28 = Utf8 LASyntax2$; | |
#29 = NameAndType #27:#28 // MODULE$:LASyntax2$; | |
#30 = Fieldref #26.#29 // ASyntax2$.MODULE$:LASyntax2$; | |
#31 = Utf8 A$ | |
#32 = Class #31 // A$ | |
#33 = Utf8 LA$; | |
#34 = NameAndType #27:#33 // MODULE$:LA$; | |
#35 = Fieldref #32.#34 // A$.MODULE$:LA$; | |
#36 = NameAndType #9:#11 // a:()LA; | |
#37 = Methodref #2.#36 // B.a:()LA; | |
#38 = Utf8 toASyntax2 | |
#39 = Utf8 (LA;)LA; | |
#40 = NameAndType #38:#39 // toASyntax2:(LA;)LA; | |
#41 = Methodref #32.#40 // A$.toASyntax2:(LA;)LA; | |
#42 = Utf8 once$extension | |
#43 = Utf8 (LA;)I | |
#44 = NameAndType #42:#43 // once$extension:(LA;)I | |
#45 = Methodref #26.#44 // ASyntax2$.once$extension:(LA;)I | |
#46 = Utf8 A$ASyntax$ | |
#47 = Class #46 // A$ASyntax$ | |
#48 = Utf8 LA$ASyntax$; | |
#49 = NameAndType #27:#48 // MODULE$:LA$ASyntax$; | |
#50 = Fieldref #47.#49 // A$ASyntax$.MODULE$:LA$ASyntax$; | |
#51 = Utf8 ASyntax | |
#52 = NameAndType #51:#39 // ASyntax:(LA;)LA; | |
#53 = Methodref #32.#52 // A$.ASyntax:(LA;)LA; | |
#54 = Utf8 twice$extension | |
#55 = NameAndType #54:#43 // twice$extension:(LA;)I | |
#56 = Methodref #47.#55 // A$ASyntax$.twice$extension:(LA;)I | |
#57 = Utf8 ASyntax$ | |
#58 = Utf8 Code | |
#59 = Utf8 LocalVariableTable | |
#60 = Utf8 LineNumberTable | |
#61 = Utf8 SourceFile | |
#62 = Utf8 InnerClasses | |
#63 = Utf8 RuntimeVisibleAnnotations | |
#64 = Utf8 ScalaSig | |
{ | |
public A a(); | |
flags: ACC_PUBLIC | |
Code: | |
stack=1, locals=1, args_size=1 | |
0: aload_0 | |
1: getfield #13 // Field a:LA; | |
4: areturn | |
LocalVariableTable: | |
Start Length Slot Name Signature | |
0 5 0 this LB; | |
LineNumberTable: | |
line 2: 0 | |
public B(); | |
flags: ACC_PUBLIC | |
Code: | |
stack=4, locals=1, args_size=1 | |
0: aload_0 | |
1: invokespecial #19 // Method java/lang/Object."<init>":()V | |
4: aload_0 | |
5: new #21 // class A | |
8: dup | |
9: iconst_1 | |
10: invokespecial #24 // Method A."<init>":(I)V | |
13: putfield #13 // Field a:LA; | |
16: getstatic #30 // Field ASyntax2$.MODULE$:LASyntax2$; | |
19: getstatic #35 // Field A$.MODULE$:LA$; | |
22: aload_0 | |
23: invokevirtual #37 // Method a:()LA; | |
26: invokevirtual #41 // Method A$.toASyntax2:(LA;)LA; | |
29: invokevirtual #45 // Method ASyntax2$.once$extension:(LA;)I | |
32: pop | |
33: getstatic #50 // Field A$ASyntax$.MODULE$:LA$ASyntax$; | |
36: getstatic #35 // Field A$.MODULE$:LA$; | |
39: aload_0 | |
40: invokevirtual #37 // Method a:()LA; | |
43: invokevirtual #53 // Method A$.ASyntax:(LA;)LA; | |
46: invokevirtual #56 // Method A$ASyntax$.twice$extension:(LA;)I | |
49: pop | |
50: return | |
LocalVariableTable: | |
Start Length Slot Name Signature | |
0 51 0 this LB; | |
LineNumberTable: | |
line 5: 0 | |
line 2: 4 | |
line 3: 19 | |
line 4: 36 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment