Kotlin Koans 07 Nullable types

Nullable types (Playground)

Description

Read about null safety and safe calls in Kotlin and rewrite the following Java code using only one if expression:

Kotlinの null安全と安全呼び出しを読み、次のJavaコードを1つの if 式に書き換えてください。

public void sendMessageToClient(
@Nullable Client client,
@Nullable String message,
@NotNull Mailer mailer
) {
if (client == null || message == null) return;

PersonalInfo personalInfo = client.getPersonalInfo();
if (personalInfo == null) return;

String email = personalInfo.getEmail();
if (email == null) return;

mailer.sendMessage(email, message);
}

Code

fun sendMessageToClient(
client: Client?, message: String?, mailer: Mailer
){
val email = client?.personalInfo?.email
if (email != null && message != null) {
mailer.sendMessage(email, message)
}
}

class Client (val personalInfo: PersonalInfo?)
class PersonalInfo (val email: String?)
interface Mailer {
fun sendMessage(email: String, message: String)
}

← Blog