allow nullable expressions to be used for lazily creating new items if there is not a value in the Map
This commit is contained in:
@@ -34,7 +34,7 @@ val <K,V> Map.Entry<K,V>.value : V
|
|||||||
*
|
*
|
||||||
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
* @includeFunctionBody ../../test/MapTest.kt getOrElse
|
||||||
*/
|
*/
|
||||||
public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
|
public inline fun <K,V> Map<K,V>.getOrElse(key: K, defaultValue: ()-> V?) : V? {
|
||||||
val current = this.get(key)
|
val current = this.get(key)
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
return current
|
return current
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ class CollectionTest {
|
|||||||
assertEquals(4, byLength.size())
|
assertEquals(4, byLength.size())
|
||||||
|
|
||||||
val l3 = byLength.getOrElse(3, {ArrayList<String>()})
|
val l3 = byLength.getOrElse(3, {ArrayList<String>()})
|
||||||
assertEquals(2, l3.size)
|
assertEquals(2, l3?.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
test fun makeString() {
|
test fun makeString() {
|
||||||
|
|||||||
Reference in New Issue
Block a user