stdlib: Add assert() functions.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Throws an [AssertionError] if the [value] is false
|
||||
* and runtime assertions have been enabled during compilation.
|
||||
*/
|
||||
public inline fun assert(value: Boolean) {
|
||||
assert(value) { "Assertion failed" }
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [AssertionError] calculated by [lazyMessage] if the [value] is false
|
||||
* and runtime assertions have been enabled during compilation.
|
||||
*/
|
||||
public inline fun assert(value: Boolean, lazyMessage: () -> Any) {
|
||||
if (!value) {
|
||||
val message = lazyMessage()
|
||||
throw AssertionError(message)
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,9 @@ public class AssertionError : Error {
|
||||
constructor(message: String) : super(message) {
|
||||
}
|
||||
|
||||
constructor(message: Any) : super(message.toString()) {
|
||||
}
|
||||
|
||||
constructor(message: String, cause: Throwable) : super(message, cause) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user