In Scala we have a require method that is used to set preconditions to 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 like that in Kotlin?
Contrary to Scala, it does not require a different mindset. Java developers feel comfortable with the language in a second and are able to code productively much faster. Having Java and Kotlin side-by-side in a project is no hassle due to extensive compatibility and platform types.
It built on people’s experience with Scala. A common complaint with Scala is slow compilation time, and Kotlin offers compile speeds comparable to Java. It’s recently gotten a big boost from Google, which has declared it a first-class language for Android development.
Though both Scala and Kotlin are interoperable with Java, Kotlin leads the show if you wish to maintain full compatibility with existing Java-based projects and technologies. Kotlin is designed to be 100% interoperable with Java. So, you can easily call Kotlin code from Java and vice-versa effortlessly.
Scala provides strong support for XML. You can put XML directly into Scala code and assign it to an XML object. This creates complications, since a < operator that isn’t followed by a space may be read as the start of an XML expression. Kotlin uses the more traditional approach of classes to handle XML objects.
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 a requireNotNull
, check
, checkNotNull
, assert
.
There are also various other assert methods in kotlin-test.
How about Preconditions.kt or Assert ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With