【问题标题】:Scala require() equivalent in KotlinKotlin 中的 Scala require() 等价物
【发布时间】:2016-07-05 15:18:04
【问题描述】:

在 Scala 中,我们有一个 require 方法,用于为这样的类设置前置条件

class Rational(x: Int, y: Int) {
   require(y != 0, "denominator must be different than zero ")

我的问题是:我们在 Kotlin 中有类似的东西吗?

【问题讨论】:

    标签: scala class require kotlin


    【解决方案1】:

    Kotlin 标准库也有一个require 方法:

    class Rational(x: Int, y: Int) {
        init {
            require(y != 0) { "denominator must be different than zero " }
        }
    }
    

    它还有一个requireNotNullcheckcheckNotNullassert

    kotlin-test 中还有其他各种断言方法。

    【讨论】:

      【解决方案2】:

      Preconditions.ktAssert 怎么样?

      【讨论】:

        猜你喜欢
        • 2019-01-18
        • 1970-01-01
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 2015-08-14
        • 1970-01-01
        • 2022-09-27
        相关资源
        最近更新 更多