From 659fceecbe021030c89a7a75d99f01bbb734bdda Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 8 Feb 2012 11:45:59 +0000 Subject: [PATCH] removed unnecessary getOrElse(value) method as we can use ?: (which is neater!) though shame it doesn't work for lazy values, passing a function body which is invoked if the value is not null --- stdlib/ktSrc/Standard.kt | 11 +---------- stdlib/ktSrc/dom/Dom.kt | 25 +++++++++++++++++-------- testlib/test/GetOrElseTest.kt | 21 +++++++++++++++++---- testlib/test/dom/DomBuilderTest.kt | 2 ++ testlib/test/dom/DomTest.kt | 1 - 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/stdlib/ktSrc/Standard.kt b/stdlib/ktSrc/Standard.kt index ce1753db5e0..26183d6be81 100644 --- a/stdlib/ktSrc/Standard.kt +++ b/stdlib/ktSrc/Standard.kt @@ -70,19 +70,10 @@ Run function f */ inline fun run(f: () -> T) = f() -/* -Allow a default value to be provided when converting a nullable type to a non-nullable type -*/ -inline fun T?.getOrElse(defaultValue: T): T { - return if (this != null) - this - else - defaultValue -} - /* Allow a default value to be lazily provided from a function when converting a nullable type to a non-nullable type */ +// TODO would be nice to replace this with the ?: notation instead allowing a function as an argument for the default :) inline fun T?.getOrElse(defaultValueFactory: ()-> T): T { return if (this != null) this diff --git a/stdlib/ktSrc/dom/Dom.kt b/stdlib/ktSrc/dom/Dom.kt index 5497b8cbdc0..268d58d4a2a 100644 --- a/stdlib/ktSrc/dom/Dom.kt +++ b/stdlib/ktSrc/dom/Dom.kt @@ -7,20 +7,20 @@ import org.w3c.dom.* // Properties var Element.id : String -get() = this.getAttribute("id").getOrElse("") +get() = this.getAttribute("id")?: "" set(value) { this.setAttribute("id", value) } var Element.style : String -get() = this.getAttribute("style").getOrElse("") +get() = this.getAttribute("style")?: "" set(value) { this.setAttribute("style", value) } // TODO can we come up with a better name; 'class' is a reserved word? var Element.cssClass : String -get() = this.getAttribute("class").getOrElse("") +get() = this.getAttribute("class")?: "" set(value) { this.setAttribute("class", value) } @@ -34,6 +34,10 @@ inline fun Node.plus(child: Node?): Node { return this } +inline fun Element.plus(text: String?): Element = this.addText(text) + +inline fun Element.plusAssign(text: String?): Element = this.addText(text) + // Builder @@ -58,8 +62,11 @@ fun Element.createElement(name: String, doc: Document? = null, init: Element.()- /* Returns the owner document of the element or uses the provided document */ -fun Element.ownerDocument(doc: Document? = null): Document { - val answer = if (doc == null) this.getOwnerDocument() else doc +fun Node.ownerDocument(doc: Document? = null): Document { + val answer = if (this is Document) this as Document + else if (doc == null) this.getOwnerDocument() + else doc + if (answer == null) { throw IllegalArgumentException("Element does not have an ownerDocument and none was provided for: ${this}") } else { @@ -88,8 +95,10 @@ fun Element.addElement(name: String, doc: Document? = null, init: Element.()-> U /* Adds a newly created text node to an element which either already has an owner Document or one must be provided as a parameter */ -fun Element.addText(text: String, doc: Document? = null): Element { - val child = ownerDocument(doc).createTextNode(text) - this.appendChild(child) +fun Element.addText(text: String?, doc: Document? = null): Element { + if (text != null) { + val child = ownerDocument(doc).createTextNode(text) + this.appendChild(child) + } return this } diff --git a/testlib/test/GetOrElseTest.kt b/testlib/test/GetOrElseTest.kt index fef7087fa65..c580628e103 100644 --- a/testlib/test/GetOrElseTest.kt +++ b/testlib/test/GetOrElseTest.kt @@ -8,24 +8,37 @@ class GetOrElseTest() : TestSupport() { val v2: String? = null fun testDefaultValue() { - assertEquals("hello", v1.getOrElse("bar")) + assertEquals("hello", v1?: "bar") expect("hello") { - v1.getOrElse("bar") + v1?: "bar" } } fun testDefaultValueOnNull() { - assertEquals("bar", v2.getOrElse("bar")) + assertEquals("bar", v2?: "bar") expect("bar") { - v2.getOrElse("bar") + v2?: "bar" } } + /** TODO not supported yet? + fun testLazyDefaultValue() { var counter = 0 + assertEquals("hello", v1?: { counter++; "bar"}) + assertEquals(counter, 0, "counter should not be incremented yet") + + assertEquals("bar", v2?: { counter++; "bar"}) + assertEquals(counter, 1, "counter should be incremented in the default function") + } + */ + + fun testLazyDefaultValueUsingMethod() { + var counter = 0 + assertEquals("hello", v1.getOrElse{ counter++; "bar"}) assertEquals(counter, 0, "counter should not be incremented yet") diff --git a/testlib/test/dom/DomBuilderTest.kt b/testlib/test/dom/DomBuilderTest.kt index 647852336ea..fb4cd426671 100644 --- a/testlib/test/dom/DomBuilderTest.kt +++ b/testlib/test/dom/DomBuilderTest.kt @@ -19,6 +19,8 @@ class DomBuilderTest() : TestSupport() { addElement("grandChild") { cssClass = "tiny" addText("Hello World!") + // TODO support neater syntax sugar for adding text? + // += "Hello World!" } } } diff --git a/testlib/test/dom/DomTest.kt b/testlib/test/dom/DomTest.kt index 0f09aadc17f..1198b4f7011 100644 --- a/testlib/test/dom/DomTest.kt +++ b/testlib/test/dom/DomTest.kt @@ -4,7 +4,6 @@ import std.* import std.dom.* import stdhack.test.* import org.w3c.dom.* -import std.dom.toXmlString class DomTest() : TestSupport() {