Add EXACTLY_ONCE contract to functions that call their lambda parameter once

KT-35972
This commit is contained in:
Ilya Gorbunov
2020-07-01 18:47:44 +03:00
parent c85432b2f9
commit 1a32fdf6d7
5 changed files with 53 additions and 21 deletions
@@ -6,19 +6,25 @@
package kotlinx.dom
import org.w3c.dom.*
import kotlin.contracts.*
/**
* Creates a new element with the specified [name].
*
* The element is initialized with the specified [init] function.
*/
public fun Document.createElement(name: String, init: Element.() -> Unit): Element = createElement(name).apply(init)
public fun Document.createElement(name: String, init: Element.() -> Unit): Element {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
return createElement(name).apply(init)
}
/**
* Appends a newly created element with the specified [name] to this element.
*
* The element is initialized with the specified [init] function.
*/
public fun Element.appendElement(name: String, init: Element.() -> Unit): Element =
ownerDocument!!.createElement(name, init).also { appendChild(it) }
public fun Element.appendElement(name: String, init: Element.() -> Unit): Element {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
return ownerDocument!!.createElement(name, init).also { appendChild(it) }
}