Reputation: 26084
In Java, I would do something like this:
class Person {
private Record record;
public String name() {
record().get("name");
}
private Record record() {
if (record == null) {
refetch();
}
return record;
}
private void refetch() {
record = service.doSomething()
}
}
In Kotlin, I have this equivalent code:
class Person(private var record: Record?) {
fun name() : String {
record().get("name");
}
private fun record() : Record {
record ?: refetch()
return record!!;
}
private fun refetch() {
record = service.doSomething()
}
}
As you see, I'm using !!
operator, and I don't really like. Is there another more idiomatic way to do this?
If I simply follow the Java way (if (record == null)
), I get this error:
Smart cast to "Record" is impossible, because "record" is a mutable property that could have been changed by this time
Upvotes: 2
Views: 214
Reputation: 839
You're Java code didn't compile so I mocked up an example.
class Record {
String name;
String address;
Record(String name, String address) {
this.name = name;
this.address = address;
}
String getName() {
return name;
}
}
class Person {
private Record record;
public String name() {
return record().getName();
}
private Record record() {
if (record == null) {
refetch();
}
return record;
}
private void refetch() {
record = new Record("Joe Smith", "101 Broadway, NYC, NY");
}
}
Kotlin forces you to DECLARE if a value is nullable by using the '?' char when you define it. So it needs to look like this:
internal class Record(var name: String, var address: String)
internal class Person {
private var record: Record? = null
fun name(): String {
return record()!!.name
}
private fun record(): Record? {
if (record == null) {
refetch()
}
return record
}
private fun refetch() {
record = Record("Joe Smith", "101 Broadway, NYC, NY")
}
}
Upvotes: 0
Reputation: 692231
In idiomatic Kotlin, you would use a lazy delegated property:
class Person(private val service: Service) {
private val record by lazy { service.doSomething() }
fun name() = record.get("name");
}
Upvotes: 3