gpt4 book ai didi

kotlin - Kotlin 中的前置条件函数 - 良好实践

转载 作者:IT老高 更新时间:2023-10-28 13:37:56 26 4
gpt4 key购买 nike

作为一名 Kotlin 编码新手,我想知道是否有一些好的做法甚至语言结构可以在函数中声明前置条件。

在 Java 中,我一直在使用 Guava 的前提条件检查实用程序:

https://github.com/google/guava/wiki/PreconditionsExplained

经过进一步调查,我发现了 require 函数:

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html

这是通常用于检查函数先决条件的方法吗?

最佳答案

当然。您可以在 Preconditions.kt 中找到所有前提条件。 .除了require函数,有requireNotNull , check & checkNotNull功能。

由于文档在 Kotlin 中的描述很差,但您可以看到 Objects#requireNonNull jdk 中的文档作为进一步。

Checks that the specified object reference is not null. This method is designed primarily for doing parameter validation in methods and constructors.

关于kotlin - Kotlin 中的前置条件函数 - 良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45131330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com