From 9f546f938d757087a41ac8a96cd3d9d7429771fb Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Mon, 4 Feb 2013 17:29:56 +0400 Subject: [PATCH] KT-3192 Drop kotlin.nullable package #KT-3192 Fixed --- .../basic/ExtensionFromStandardLibrary.kt | 3 +- libraries/stdlib/src/kotlin/Tuples.kt | 15 +- .../stdlib/src/kotlin/nullable/Nullables.kt | 184 ------------------ libraries/stdlib/test/TuplesTest.kt | 10 +- .../stdlib/test/language/scala/OptionTest.kt | 147 -------------- .../stdlib/test/language/scala/ReadMe.md | 3 - 6 files changed, 14 insertions(+), 348 deletions(-) delete mode 100644 libraries/stdlib/src/kotlin/nullable/Nullables.kt delete mode 100644 libraries/stdlib/test/language/scala/OptionTest.kt delete mode 100644 libraries/stdlib/test/language/scala/ReadMe.md diff --git a/idea/testData/completion/basic/ExtensionFromStandardLibrary.kt b/idea/testData/completion/basic/ExtensionFromStandardLibrary.kt index 2ee71c88785..898d674715a 100644 --- a/idea/testData/completion/basic/ExtensionFromStandardLibrary.kt +++ b/idea/testData/completion/basic/ExtensionFromStandardLibrary.kt @@ -10,5 +10,4 @@ fun firstFun() { // RUNTIME: 1 // TIME: 1 // EXIST: toLinkedList@toLinkedList()~for jet.Iterable in kotlin -// EXIST: toLinkedList@toLinkedList()~for T? in kotlin.nullable -// NUMBER: 2 \ No newline at end of file +// NUMBER: 1 diff --git a/libraries/stdlib/src/kotlin/Tuples.kt b/libraries/stdlib/src/kotlin/Tuples.kt index b39cbf853ca..db6ed502bdf 100644 --- a/libraries/stdlib/src/kotlin/Tuples.kt +++ b/libraries/stdlib/src/kotlin/Tuples.kt @@ -1,8 +1,9 @@ package kotlin -import kotlin.nullable.hashCodeOrDefault import java.io.Serializable +private fun Any?.safeHashCode() : Int = if (this == null) 0 else this.hashCode() + // TODO: make it a data class public class Pair ( public val first: A, @@ -14,8 +15,8 @@ public class Pair ( override fun toString(): String = "($first, $second)" override fun hashCode(): Int { - var result = first.hashCodeOrDefault(0) - result = 31 * result + second.hashCodeOrDefault(0) + var result = first.safeHashCode() + result = 31 * result + second.safeHashCode() return result; } @@ -40,9 +41,9 @@ public class Triple ( override fun toString(): String = "($first, $second, $third)" override fun hashCode(): Int { - var result = first.hashCodeOrDefault(0) - result = 31 * result + second.hashCodeOrDefault(0) - result = 31 * result + third.hashCodeOrDefault(0) + var result = first.safeHashCode() + result = 31 * result + second.safeHashCode() + result = 31 * result + third.safeHashCode() return result; } @@ -55,4 +56,4 @@ public class Triple ( second == t.second && third == t.third; } -} \ No newline at end of file +} diff --git a/libraries/stdlib/src/kotlin/nullable/Nullables.kt b/libraries/stdlib/src/kotlin/nullable/Nullables.kt deleted file mode 100644 index aad110f069d..00000000000 --- a/libraries/stdlib/src/kotlin/nullable/Nullables.kt +++ /dev/null @@ -1,184 +0,0 @@ -package kotlin.nullable - -import java.util.* - -/** Returns true if the element is not null and matches the given predicate */ -public inline fun T?.any(predicate: (T)-> Boolean): Boolean { - return this != null && predicate(this) -} - -/** Returns true if the element is not null and matches the given predicate */ -public inline fun T?.all(predicate: (T)-> Boolean): Boolean { - return this != null && predicate(this) -} - -/** Returns the 1 if the element is not null else 0 */ -public inline fun T?.count(predicate: (T)-> Boolean): Int { - return if (this != null) 1 else 0 -} - -/** Returns the first item which matches the predicate if this element is not null else null */ -public inline fun T?.find(predicate: (T)-> Boolean): T? { - return if (this != null && predicate(this)) this else null -} - -/** Returns a new List containing all elements in this collection which match the given predicate */ -public inline fun T?.filter(predicate: (T)-> Boolean): T? = find(predicate) - -/** Filters all elements in this collection which match the given predicate into the given result collection */ -public inline fun > T?.filterTo(result: C, predicate: (T)-> Boolean): C { - if (this != null && predicate(this)) - result.add(this) - return result -} - -/** Returns a List containing all the non null elements in this collection */ -public inline fun T?.filterNotNull(): Collection = filterNotNullTo(java.util.ArrayList()) - -/** Filters all the null elements in this collection winto the given result collection */ -public inline fun > T?.filterNotNullTo(result: C): C { - if (this != null) { - result.add(this) - } - return result -} - -/** Returns a new collection containing all elements in this collection which do not match the given predicate */ -public inline fun T?.filterNot(predicate: (T)-> Boolean): Collection = filterNotTo(ArrayList(), predicate) - -/** Returns a new collection containing all elements in this collection which do not match the given predicate */ -public inline fun > T?.filterNotTo(result: C, predicate: (T)-> Boolean): C { - if (this != null && !predicate(this)) { - result.add(this) - } - return result -} - -/** - * Returns the result of transforming each item in the collection to a one or more values which - * are concatenated together into a single collection - */ -public inline fun T?.flatMap(transform: (T)-> MutableCollection): Collection { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns the result of transforming each item in the collection to a one or more values which - * are concatenated together into a single collection - */ -public inline fun T?.flatMapTo(result: MutableCollection, transform: (T)-> MutableCollection): Collection { - if (this != null) { - val coll = transform(this) - for (r in coll) { - result.add(r) - } - } - return result -} - -/** Performs the given operation on each element inside the collection */ -public inline fun T?.forEach(operation: (element: T) -> Unit) { - if (this != null) { - operation(this) - } -} - -/** - * Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values - * - * For example to sum together all numeric values in a collection of numbers it would be - * {code}val total = numbers.fold(0){(a, b) -> a + b}{code} - */ -public inline fun T?.fold(initial: T, operation: (it: T, it2: T) -> T): T { - return if (this != null) { - operation(initial, this) - } else { - initial - } -} - -/** - * Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values - */ -public inline fun T?.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - // maximum size is 1 so reverse is not needed - return fold(initial, {x, y -> operation(y, x)}) -} - -/** - * Iterates through the collection performing the transformation on each element and using the result - * as the key in a map to group elements by the result - */ -public inline fun T?.groupBy(result: MutableMap> = HashMap>(), toKey: (T)-> K): Map> { - if (this != null) { - val key = toKey(this) - val list = result.getOrPut(key){ ArrayList() } - list.add(this) - } - return result -} - - -/** Creates a String from the nullable or item with the given prefix and postfix if supplied */ -public inline fun T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String { - val buffer = StringBuilder(prefix) - if (this != null) { - buffer.append(this) - } - buffer.append(postfix) - return buffer.toString() -} - - -/** Returns the nullable result of transforming this with the given transformation function */ -public inline fun T?.map(transform : (T) -> R) : R? { - return if (this != null) { - transform(this) - } else { - null - } -} - -/** Transforms each element of this collection with the given function then adds the results to the given collection */ -public inline fun > T?.mapTo(result: C, transform : (T) -> R) : C { - if (this != null) { - result.add(transform(this)) - } - return result -} - -/** Returns itself since it can't be reversed as it can contain at most one item */ -public inline fun T?.reverse(): T? { - return this -} - -/** Copies the collection into the given collection */ -public inline fun > T?.toCollection(result: C): C { - if (this != null) - result.add(this) - return result -} - -/** Converts the collection into a LinkedList */ -public inline fun T?.toLinkedList(): LinkedList = this.toCollection(LinkedList()) - -/** Converts the collection into a List */ -public inline fun T?.toList(): List = this.toCollection(ArrayList()) - -/** Converts the collection into a Set */ -public inline fun T?.toSet(): Set = this.toCollection(HashSet()) - -/** Converts the collection into a SortedSet */ -public inline fun T?.toSortedSet(): SortedSet = this.toCollection(TreeSet()) -/** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun T?.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer -} -*/ - - -/** Returns a hash code on an existing object, or default value otherwise */ - public inline fun Any?.hashCodeOrDefault(default: Int): Int = if (this == null) default else this.hashCode() diff --git a/libraries/stdlib/test/TuplesTest.kt b/libraries/stdlib/test/TuplesTest.kt index f0a068e29dd..d857edd3857 100644 --- a/libraries/stdlib/test/TuplesTest.kt +++ b/libraries/stdlib/test/TuplesTest.kt @@ -8,18 +8,18 @@ class PairTest { val p = Pair(1, "a") test fun pairFirstAndSecond() { - assertTrue(p.first == 1) - assertTrue(p.second == "a") + assertEquals(1, p.first) + assertEquals("a", p.second) } test fun pairMultiAssignment() { val (a, b) = p - assertTrue(a == 1) - assertTrue(b == "a") + assertEquals(1, a) + assertEquals("a", b) } test fun pairToString() { - assertTrue(p.toString() == "(1, a)") + assertEquals("(1, a)", p.toString()) } test fun pairEquals() { diff --git a/libraries/stdlib/test/language/scala/OptionTest.kt b/libraries/stdlib/test/language/scala/OptionTest.kt deleted file mode 100644 index 8f5a3af41c1..00000000000 --- a/libraries/stdlib/test/language/scala/OptionTest.kt +++ /dev/null @@ -1,147 +0,0 @@ -package language.scala - -import kotlin.nullable.* - -import junit.framework.TestCase -import kotlin.test.assertEquals - -class Request(val value: String?) { - fun getParameter(name: String): String? { - return value - } -} - -/** - * This test case shows how we can use T?, the Kotlin nullable type instead of Option[T] in Scala - * - * Its worth saying that nullable types have 2 huge benefits over Option: - * - * * Already works with any Java or JVM based API which can return nulls - * * No extra object construction to wrap non-null values - * - * Examples taken from the [Scala API docs for Option](http://www.scala-lang.org/api/current/scala/Option.html) - * - * Composition of nullable types is currently implemented with the optional kotlin.nullable package - */ -class OptionTest: TestCase() { - - fun testPatternMatching() { - fun foo(request: Request): String { - - /* Scala: - - val nameMaybe = request.getParameter("name") - nameMaybe match { - case Some(name) => { - name.trim.toUppercase - } - case None => { - "No name value" - } - } - */ - - // Kotlin version: - val name = request.getParameter("name") - return when (name) { - is String -> { - name.trim().toUpperCase() - } - else -> { - "No name value" - } - } - } - - assertEquals("No name value", foo(Request(null))) - assertEquals("BAR", foo(Request("BAR"))) - assertEquals("BAR", foo(Request(" bar "))) - - println("foo(null) = ${foo(Request(null))}") - println("foo(\" bar \") = ${foo(Request(" bar "))}") - } - - fun testPatternMatchingUsingIf() { - fun foo(request: Request): String { - - /* Scala: - - val nameMaybe = request.getParameter("name") - nameMaybe match { - case Some(name) => { - name.trim.toUppercase - } - case None => { - "No name value" - } - } - */ - - // Kotlin version - val name = request.getParameter("name") - return if (name != null) { - name.trim().toUpperCase() - } else { - "No name value" - } - } - - assertEquals("No name value", foo(Request(null))) - assertEquals("BAR", foo(Request("BAR"))) - assertEquals("BAR", foo(Request(" bar "))) - - println("foo(Request(null)) = ${foo(Request(null))}") - println("foo(Request(\" bar \")) = ${foo(Request(" bar "))}") - } - - - fun testFunctionComposition() { - fun foo(request: Request): String { - /* Scala: - - val name:Option[String] = request.getParameter("name") - val upper = name map { _.trim } filter { _.length != 0 } map { _.toUpperCase } - println(upper.getOrElse("")) - */ - - val name = request.getParameter("name") - val upper = name.map{ it.trim() }.filter{ it.length != 0 }.map{ it.toUpperCase() } - return upper ?: "" - } - - assertEquals("", foo(Request(null))) - assertEquals("", foo(Request(" "))) - assertEquals("BAR", foo(Request(" bar "))) - } - - - fun testCompositionWithFor() { - fun foo(request: Request): String { - /* Scala: - - val upper = for { - name <- request.getParameter("name") - trimmed <- Some(name.trim) - upper <- Some(trimmed.toUpperCase) if trimmed.length != 0 - } yield upper - println(upper.getOrElse("")) - */ - - // Kotlin version - // not as clean as we've no way to compose if statements so have - // to cheat and use returns - val name = request.getParameter("name") - if (name != null) { - val trimmed = name.trim() - if (trimmed.length() != 0) { - return trimmed.toUpperCase() - } - } - return "" - } - - assertEquals("", foo(Request(null))) - assertEquals("", foo(Request(""))) - assertEquals("BAR", foo(Request(" bar "))) - } -} \ No newline at end of file diff --git a/libraries/stdlib/test/language/scala/ReadMe.md b/libraries/stdlib/test/language/scala/ReadMe.md deleted file mode 100644 index 77cbcefeebf..00000000000 --- a/libraries/stdlib/test/language/scala/ReadMe.md +++ /dev/null @@ -1,3 +0,0 @@ -This package compares and contrasts some Scala coding patterns with how things work in Kotlin. - -Its more intended to help show folks familiar with doing things the Scala way, how the code would look in Kotlin. \ No newline at end of file