[kotlin] I'd like to ask you a question about the type mismatch of non-null type return

Asked 2 years ago, Updated 2 years ago, 91 views

fun getTitle(): String = titleData.joinToString(" / ")

title = bpCV?.Error if getTitle().toString() // 1 toString is missing
bpCV?.apply { title = getTitle() } // 2
title = run { bpCV?.getTitle()} // 3 error. type mismatch, Elvis operator enabled
title = bpCV?.run { getTitle()} // 4 error. type mismatch, Elvis operator enabled

This code stores the return value of getTitle in a String-type title

There are no errors for No.1 and No.2. In the case of number 1, if there is no toString(), there is an error due to type mismatch.

I'm curious about this type mismatch error.

I stated that getTitle() returns String, right? And the title is also a String type.

But the type I found in that method is called String? due to the type mismatch error.

If getTitle() specifies that it returns a String, shouldn't the type be correct?

It says that joinToString() also returns the String type.Then shouldn't the String type be returned for sure?

I don't understand why you're making a disagreement.

Is it because there is a possibility that the return value is null regardless of that?

So, in the case of number 1, you have to do getTitle() and toString, so even if it's a null value, do you return it to the String type for all values?

I used an application for number 2, but I don't think it's a good idea because the application uses it to initialize my properties

I think it's better to write code like number 3 or 4, but I don't know how to guarantee the string.

kotlin

2022-09-20 15:50

1 Answers

The questioner seems to be confused about the cause of the type mismatch in that the characteristics of Kotlin's safety call and toString() return type and getTitle() return type are the same :)

First, bpCV?bpCV means nullable, so bpCV will not be able to return the null to the instance without null. Therefore, bpCV?GetTitle() itself means nullable. Therefore, if title is non-null type, it is correct that a type mismatch occurs. This safety call is used to ensure stability for NullPointerException.

Second, let's look at the toString() internal code.

/**
 * * Returns a string representation of the object. Can be called with a null receiver, in which case
 * * it returns the string "null".
 */
public fun Any?.toString(): String

bpCV?.toString() above toString() is a null receiver extension function for any?, so will match to as it is. Please refer to kotlin extensions for more information.

Next,

I don't think it's a good idea just because the application uses it to initialize your properties, what do you think?

It is true that the application configuration can be used easily when initializing properties, but it must not be used only in the relevant situation.

These scope functions are provided to generate temporary blocks using a particular lambda expression and make the code simpler and easier to read, so look at the characteristics of those functions and use them appropriately where necessary.

Therefore, as shown in #2, bpCV?The apply { title = getTitle()} code is also a good idea. You can also use the Elvis operator to handle it.

title = bpCV?.getTitle() : "Title"
title = bpCV?.gettitle() ?: throw Exception("Unknown title")

I think you may have understood the part where the type discrepancy occurs in No. 3 and No. 4.


2022-09-20 15:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.