android - Initializing field that has getter and setter? -


i created class has example field in kotlin

class someclass {     var smth: string = "initial value"             get() = "here is"             set(value) {                 field = "it $value"             } } 

when create object of class , call smth field, call get() property anyway.

        val myvalue = someclass().smth// myvalue = "here is" 

so, question is: why have initialize field has getter?

 var smth: string // why gives error?         get() = "here is"         set(value) {             field = "it $value"         } 

it return value get() property, doesn't it?

i think because compiler not smart enough infer not null.

actually similar code presented official doc here https://kotlinlang.org/docs/reference/properties.html

var stringrepresentation: string     get() = this.tostring()     set(value) {         setdatafromstring(value) // parses string , assigns values other properties     } 

apparently code won't compile either unless constructor like

constructor(stringrepresentation: string) {     this.stringrepresentation = stringrepresentation } 

is added.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -