简体   繁体   English

如何从Kotlin中的函数返回布尔值

[英]How to return boolean value from a function in Kotlin

I am new to kotlin. 我是kotlin的新手。 I am reading a key and value from properties file, in a kotlin program. 我正在kotlin程序中读取属性文件中的键和值。 But I don't know how to directly return the value of a key. 但我不知道如何直接返回一个键的值。 Please find the application.yml and abc.class(this is a kotlin class) below. 请在下面找到application.yml和abc.class(这是一个kotlin类)。

application.yml application.yml

abcconfig:
  isabcEnabled:
    default: false
    xyz: true
    def: true

abc.class abc.class

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component

@Component
@ConfigurationProperties(prefix = "abcconfig")
class AbcConfig {

    private var abcEnabled: Map<String, Boolean> = mutableMapOf()

    fun getabcEnabledValue(siteId: String?): Boolean {

        val abc: Boolean
        val key: String? = if (abcEnabled.containsKey(key)) key else "default"

        abc = abcEnabled[key]
        return abc
    }

    fun setAbcEnabled(abcEnabled: Map<String, Boolean>) {
        this.abcEnabled = abcEnabled
    }

}

This is about nullability. 这是关于可空性的。 The Kotlin compiler keeps track of whether each value could be null or not, and prevents you from doing things that would be unsafe. Kotlin编译器会跟踪每个值是否为null,并阻止您执行不安全的操作。

The code in this question has one particular nullability issue. 此问题中的代码具有一个特定的可空性问题。 (It also has some confusion, including two references to key before it's set. I'll assume those should be siteId .) (它也有一些混乱,包括在设置之前对key两次引用。我假设那些应该是siteId 。)

The issue is what happens when the abcEnabled map doesn't contain the requested key. 问题是当abcEnabled映射不包含请求的密钥时会发生什么。 If the key is present, then the […] operator will return the corresponding Boolean value; 如果密钥存在,则[…]运算符将返回相应的Boolean值; but if the key is not present (which could happen if the map doesn't contain a "default" key), it returns null. 但是如果密钥不存在(如果映射不包含“默认”密钥可能会发生),则返回null。 However, the variable you're trying to assign it to is of type Boolean , which doesn't allow nulls. 但是,您尝试将其赋值给的变量是Boolean类型,它不允许空值。 That's why the compiler complains. 这就是编译器抱怨的原因。

So you'll have to decide what you want to happen if the map contains no "default" key. 因此,如果地图不包含“默认”键,您必须决定要发生什么。 (Or find a way to ensure it always does; but that's a lot harder, especially if the method could be called before the object is fully initialised, or while another thread is setting or updating the map. So it's much safer to handle the case gracefully.) (或者找到一种方法来确保它始终如此;但这要困难得多,特别是如果可以在对象完全初始化之前调用该方法,或者在另一个线程设置或更新地图时调用该方法。因此处理案例更加安全优雅。)

If you want to return false in that case, the code could boil down to: 如果你想在这种情况下返回false ,代码可以归结为:

fun getabcEnabledValue(siteId: String?): Boolean {
    val key: String? = if (abcEnabled.containsKey(siteId)) siteId else "default"
    return abcEnabled[key] ?: false
}

or even (for better thread-safety as well as brevity and clarity): 甚至(为了更好的线程安全性以及简洁和清晰度):

fun getabcEnabledValue(siteId: String?)
    = abcEnabled[siteId] ?: abcEnabled["default"] ?: false

Or if you want to return null in that case, simply declare the function as returning Boolean? 或者如果你想在这种情况下返回null ,只需将该函数声明为返回Boolean? (which allows null) — or leave off the ?: false in the last example. (允许null) - 或者在最后一个例子中?: false

(Also, as a matter of style, I'm not sure why you've made abcEnabled a private property and then added your own setter. Is it really necessary to hide the getter? If not, a public property would be simpler. And it's probably worth making the capitalisation of abc in the method names consistent.) (另外,作为一种风格问题,我不确定你为什么把abcEnabled变成私人财产,然后添加了你自己的setter。是否真的有必要隐藏吸气剂?如果没有,公共财产会更简单。它可能值得在方法名称abc的大写一致。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM