From 7c0fc11b4a4c9d0a8f9650aa2a275a625fbd3ea9 Mon Sep 17 00:00:00 2001 From: Artem Zinnatullin Date: Mon, 21 May 2018 12:47:44 -0700 Subject: [PATCH 1/2] toNullable() specialization. --- .../src/main/kotlin/com/gojuno/koptional/Optional.kt | 10 ++++++---- .../test/kotlin/com/gojuno/koptional/OptionalSpec.kt | 5 ++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/koptional/src/main/kotlin/com/gojuno/koptional/Optional.kt b/koptional/src/main/kotlin/com/gojuno/koptional/Optional.kt index e8e4a10..45e334e 100644 --- a/koptional/src/main/kotlin/com/gojuno/koptional/Optional.kt +++ b/koptional/src/main/kotlin/com/gojuno/koptional/Optional.kt @@ -2,18 +2,20 @@ package com.gojuno.koptional sealed class Optional { - fun toNullable(): T? = when (this) { - is Some -> value - is None -> null - } + /** + * Converts [Optional] to either its non-null value if it's [Some] or `null` if it's [None]. + */ + abstract fun toNullable(): T? } data class Some(val value: T) : Optional() { override fun toString() = "Some($value)" + override fun toNullable(): T = value } object None : Optional() { override fun toString() = "None" + override fun toNullable(): Nothing? = null } fun T?.toOptional(): Optional = if (this == null) None else Some(this) diff --git a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt index 1b5c8db..6199b3d 100644 --- a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt +++ b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt @@ -12,7 +12,7 @@ class OptionalSpec : Spek({ context("Some.toNullable") { - val result = Some("string").toNullable() + val result: String = Some("string").toNullable() it("converts it to value") { assertThat(result).isEqualTo("string") @@ -21,7 +21,7 @@ class OptionalSpec : Spek({ context("None.toNullable") { - val result = None.toNullable() + val result: Nothing? = None.toNullable() it("converts it to null") { assertThat(result as Any?).isNull() @@ -80,7 +80,6 @@ class OptionalSpec : Spek({ it("converts it to String") { assertThat(result).isEqualTo("None") } - } } }) From fd63f72727eb8c5176b67224f6c6ae44a10e3678 Mon Sep 17 00:00:00 2001 From: Artem Zinnatullin Date: Mon, 21 May 2018 12:53:27 -0700 Subject: [PATCH 2/2] Add tests for Optional.toNullable(). --- .../com/gojuno/koptional/OptionalSpec.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt index 6199b3d..8c464b2 100644 --- a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt +++ b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt @@ -27,6 +27,24 @@ class OptionalSpec : Spek({ assertThat(result as Any?).isNull() } } + + context("Optional.Some.toNullable") { + + val result: String? = (Some("string") as Optional).toNullable() + + it("converts it to value") { + assertThat(result).isEqualTo("string") + } + } + + context("Optional.None.toNullable") { + + val result: Nothing? = (None as Optional).toNullable() + + it("converts it to null") { + assertThat(result as Any?).isNull() + } + } } describe("toOptional") {