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

This commit is contained in:
James Strachan
2012-02-08 11:45:59 +00:00
parent 04220935e0
commit 659fceecbe
5 changed files with 37 additions and 23 deletions
+1 -10
View File
@@ -70,19 +70,10 @@ Run function f
*/
inline fun <T> run(f: () -> T) = f()
/*
Allow a default value to be provided when converting a nullable type to a non-nullable type
*/
inline fun <T> 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> T?.getOrElse(defaultValueFactory: ()-> T): T {
return if (this != null)
this
+17 -8
View File
@@ -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
}
+17 -4
View File
@@ -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")
+2
View File
@@ -19,6 +19,8 @@ class DomBuilderTest() : TestSupport() {
addElement("grandChild") {
cssClass = "tiny"
addText("Hello World!")
// TODO support neater syntax sugar for adding text?
// += "Hello World!"
}
}
}
-1
View File
@@ -4,7 +4,6 @@ import std.*
import std.dom.*
import stdhack.test.*
import org.w3c.dom.*
import std.dom.toXmlString
class DomTest() : TestSupport() {