From 6546d7a1e7930a62c1040a345f55ee089564d368 Mon Sep 17 00:00:00 2001 From: Michael Zehender Date: Thu, 12 Dec 2013 16:14:57 +0100 Subject: [PATCH] #KT-2341 Fixed --- libraries/stdlib/src/kotlin/JUtil.kt | 2 +- libraries/stdlib/test/ListTest.kt | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index 4aaa658fdfa..690aee1c07b 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -90,7 +90,7 @@ val List.lastIndex : Int * @includeFunctionBody ../../test/ListTest.kt head */ val List.head : T? - get() = this.get(0) + get() = if (this.isNotEmpty()) this.get(0) else null /** * Returns all elements in this collection apart from the first one diff --git a/libraries/stdlib/test/ListTest.kt b/libraries/stdlib/test/ListTest.kt index 49993647dde..e972c66afdc 100644 --- a/libraries/stdlib/test/ListTest.kt +++ b/libraries/stdlib/test/ListTest.kt @@ -11,6 +11,11 @@ class ListTest { assertEquals("[foo, bar]", data.toString()) } + test fun emptyHead() { + val data = ArrayList() + assertNull(data.head) + } + test fun head() { val data = arrayList("foo", "bar") assertEquals("foo", data.head) @@ -23,6 +28,11 @@ class ListTest { assertEquals(expected, actual) } + test fun emptyFirst() { + val data = ArrayList() + assertNull(data.first) + } + test fun first() { val data = arrayList("foo", "bar") assertEquals("foo", data.first)