File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ object TestMain {
2
+ def main (args : Array [String ]): Unit = {
3
+ testExtensionProperty()
4
+ }
5
+
6
+ case class Foo (var foo : String )
7
+
8
+ object fooExt {
9
+ // define `Foo`-extension property with getter and setter delegating to `Foo.foo`
10
+ extension (thisFoo : Foo ) def fooExt : String = thisFoo.foo
11
+ extension (thisFoo : Foo ) def fooExt_= (value : String ): Unit = { thisFoo.foo = value }
12
+ }
13
+
14
+ def testExtensionProperty (): Unit = {
15
+ import fooExt ._
16
+ val foo = Foo (" initVal" )
17
+ assert(foo.fooExt == " initVal" )
18
+ foo.fooExt = " updatedVal"
19
+ assert(foo.foo == " updatedVal" )
20
+ assert(foo.fooExt == " updatedVal" )
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ object TestMain2 {
2
+ def main (args : Array [String ]): Unit = {
3
+ testExtensionProperty()
4
+ }
5
+
6
+ case class Foo (var foo : String )
7
+
8
+ implicit object fooExt {
9
+ // define `Foo`-extension property with getter and setter delegating to `Foo.foo`
10
+ extension (thisFoo : Foo ) def fooExt : String = thisFoo.foo
11
+ extension (thisFoo : Foo ) def fooExt_= (value : String ): Unit = { thisFoo.foo = value }
12
+ }
13
+
14
+ def testExtensionProperty (): Unit = {
15
+ // import fooExt._
16
+ val foo = Foo (" initVal" )
17
+ assert(foo.fooExt == " initVal" )
18
+ foo.fooExt = " updatedVal"
19
+ assert(foo.foo == " updatedVal" )
20
+ assert(foo.fooExt == " updatedVal" )
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments