I hope someone can help, I am writing moving from java to Kotlin and I want to have a property that it initialised in the init block, that has a custom setter but is non optional.
Here is my code below, however this has a warning that tag must be initialised
I would like to make it lateInit, but that means it has to be optional (which I don't want)
class SkiMarker(mapView: MapView, tag:Tag) : Marker(mapView){
var tag:Tag <- ERROR here
set(tag){
this.tag = tag
if(tag != null){
this.mPosition = GeoPoint(tag.lat,tag.lon)
}
}
init {
this.tag = tag
this.mPosition = GeoPoint(tag.lat,tag.lon)
val window = MyInfoWindow(mapView,tag)
this.infoWindow = window
window.subject.observeOn(AndroidSchedulers.mainThread()).subscribe { it ->
setIconForTracking(it)
}
}
}
Many thanks in advance for any comments
For that logic you only need to use a backing field :
class SkiMarker(mapView: MapView, tag:Tag) : Marker(mapView) {
var tag: Tag = tag
set(newTag){
field = newTag // `field` is a keyword
}
}
Note that code like set(newTag) {tag = newTag}
is equivalent to setTag(newTag: Tag) { setTag(newTag) }
where it is actually calling the setter from within itself in a recursion.
Also, tag
is never null
, so if(tag != null)
is meaningless.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.