diff --git a/stdlib/ktSrc/Standard.kt b/stdlib/ktSrc/Standard.kt index 26183d6be81..bcbd9db2ac1 100644 --- a/stdlib/ktSrc/Standard.kt +++ b/stdlib/ktSrc/Standard.kt @@ -69,14 +69,3 @@ inline fun Iterator.toTreeSet() = to(TreeSet()) Run function f */ inline fun run(f: () -> T) = f() - -/* -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 - else - defaultValueFactory() -} diff --git a/testlib/test/GetOrElseTest.kt b/testlib/test/GetOrElseTest.kt index c580628e103..755c94ae98d 100644 --- a/testlib/test/GetOrElseTest.kt +++ b/testlib/test/GetOrElseTest.kt @@ -6,6 +6,7 @@ import stdhack.test.* class GetOrElseTest() : TestSupport() { val v1: String? = "hello" val v2: String? = null + var counter = 0 fun testDefaultValue() { assertEquals("hello", v1?: "bar") @@ -23,26 +24,18 @@ class GetOrElseTest() : TestSupport() { } } - /** 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 calculateBar(): String { + counter++ + return "bar" } - */ - fun testLazyDefaultValueUsingMethod() { - var counter = 0 + fun testLazyDefaultValue() { + counter = 0 - assertEquals("hello", v1.getOrElse{ counter++; "bar"}) + assertEquals("hello", v1?: calculateBar()) assertEquals(counter, 0, "counter should not be incremented yet") - assertEquals("bar", v2.getOrElse{ counter++; "bar"}) + assertEquals("bar", v2?: calculateBar()) assertEquals(counter, 1, "counter should be incremented in the default function") } }