Gyuhyeon
12/31/2019, 8:53 AMdata class Foo(
val bar: Long? = null
)
consider this simple class. Using mybatis, a select
query from a database table is mapped into this class.
I know that mybatis by default uses default noarg constructor, then uses setters to set the value.
However, decompiling the above kotlin class reveals that bar is indeed private final Long
.
When default constructor is called, it seems to me that kotlin will use its synthetic method to automatically set bar
as null.
This would mean that bar cannot be set even with reflection magic.
Does anyone have any idea how val
fields could possibly be mapped correctly from mybatis?J茅r么me Gully
12/31/2019, 8:57 AMdata
keyword ?Gyuhyeon
12/31/2019, 8:57 AMMarc Knaup
12/31/2019, 9:01 AMval
fields are immutable by definition. If you want it to be set after the class has been instantiated you must use var
.
Foo
even has a no-arg constructor internally. It shouldn鈥檛J茅r么me Gully
12/31/2019, 9:01 AMvar bar
.
edit:Oups to lateGyuhyeon
12/31/2019, 9:05 AMGyuhyeon
12/31/2019, 9:05 AMGyuhyeon
12/31/2019, 9:06 AMMarc Knaup
12/31/2019, 9:06 AMMarc Knaup
12/31/2019, 9:07 AMMarc Knaup
12/31/2019, 9:10 AM