Scala require () equivalent in Kotlin

In Scala, we have a require method that is used to set prerequisites for classes like this

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

My question is: do we have something similar in Kotlin?

+5
source share
2 answers

Kotlin stdlib also has a require method:

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

It also has requireNotNull , check , checkNotNull , assert .

In kotlin-test, there are various other assert methods.

+10
source
+2
source

Source: https://habr.com/ru/post/1245337/


All Articles