Created
January 21, 2020 01:22
-
-
Save mpollmeier/0b2196b9d06e8e15a184a4743d649e61 to your computer and use it in GitHub Desktop.
This file contains 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
object ValueClassDebug { | |
class Foo | |
class FooExtNormalClass(val foo: Foo) { | |
def bar: Int = 42 | |
} | |
implicit class FooExtImplicitClass(val foo: Foo) { | |
def barViaImplicitClass: Int = 42 | |
} | |
implicit class FooExtImplicitValueClass(val foo: Foo) extends AnyVal { | |
def barViaImplicitValueClass: Int = 42 | |
} | |
implicit def toFooExt(foo: Foo) = new FooExtAnyVal(foo) | |
class FooExtAnyVal(val foo: Foo) extends AnyVal { | |
def barViaImplicitDef: Int = 42 | |
} | |
} | |
object Main extends App { | |
import ValueClassDebug._ | |
val foo = new Foo | |
// explicit instantiation of normal class always results in object allocation | |
// new Ext.FooExtNormalClass(this.foo()).bar(); | |
new FooExtNormalClass(foo).bar | |
// no object allocation when using implicit class (note: no value class) | |
// Ext$.MODULE$.FooExtImplicitClass(this.foo()).barViaImplicitClass(); | |
foo.barViaImplicitClass | |
// no object allocation when using implicit value class | |
// Ext.FooExtImplicitValueClass$.MODULE$.barViaImplicitValueClass$extension(Ext$.MODULE$.FooExtImplicitValueClass(this.foo())); | |
foo.barViaImplicitValueClass | |
// no object allocation when using implicit function | |
// Ext.FooExtAnyVal$.MODULE$.barViaImplicitDef$extension(Ext$.MODULE$.toFooExt(this.foo())); | |
foo.barViaImplicitDef | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment