Rename error factory method to err

Kotlin has it's own error method, therefore the IDE doesn't immediately
prompt the user to import our error factory method.
This commit is contained in:
Michael Bull 2017-10-21 15:03:39 +01:00
parent c9a7d16562
commit c3534276b4
10 changed files with 37 additions and 37 deletions

View File

@ -6,6 +6,6 @@ package com.mikebull94.result
inline fun <V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E> { inline fun <V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E> {
return when (this) { return when (this) {
is Ok -> transform(value) is Ok -> transform(value)
is Error -> error(error) is Error -> err(error)
} }
} }

View File

@ -13,7 +13,7 @@ inline fun <T, R, E> Iterable<T>.fold(
is Ok -> { is Ok -> {
accumulator = operationResult.value accumulator = operationResult.value
} }
is Error -> return error(operationResult.error) is Error -> return err(operationResult.error)
} }
} }
@ -35,7 +35,7 @@ inline fun <T, R, E> List<T>.foldRight(
is Ok -> { is Ok -> {
accumulator = operationResult.value accumulator = operationResult.value
} }
is Error -> return error(operationResult.error) is Error -> return err(operationResult.error)
} }
} }
} }
@ -55,7 +55,7 @@ fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
return ok(map { return ok(map {
when (it) { when (it) {
is Ok -> it.value is Ok -> it.value
is Error -> return error(it.error) is Error -> return err(it.error)
} }
}) })
} }

View File

@ -7,7 +7,7 @@ package com.mikebull94.result
inline fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> { inline fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
return when (this) { return when (this) {
is Ok -> ok(transform(value)) is Ok -> ok(transform(value))
is Error -> error(error) is Error -> err(error)
} }
} }
@ -18,7 +18,7 @@ inline fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
inline fun <V, E, U> Result<V, E>.mapError(transform: (E) -> U): Result<V, U> { inline fun <V, E, U> Result<V, E>.mapError(transform: (E) -> U): Result<V, U> {
return when (this) { return when (this) {
is Ok -> ok(value) is Ok -> ok(value)
is Error -> error(transform(error)) is Error -> err(transform(error))
} }
} }
@ -46,6 +46,6 @@ inline fun <V1, V2, E1, E2> Result<V1, E1>.mapEither(
): Result<V2, E2> { ): Result<V2, E2> {
return when (this) { return when (this) {
is Ok -> ok(success(value)) is Ok -> ok(success(value))
is Error -> error(failure(error)) is Error -> err(failure(error))
} }
} }

View File

@ -7,7 +7,7 @@ package com.mikebull94.result
sealed class Result<out V, out E> sealed class Result<out V, out E>
fun <V> ok(value: V) = Ok<V, Nothing>(value) fun <V> ok(value: V) = Ok<V, Nothing>(value)
fun <E> error(error: E) = Error<Nothing, E>(error) fun <E> err(error: E) = Error<Nothing, E>(error)
class Ok<out V, out E> internal constructor(val value: V) : Result<V, E>() { class Ok<out V, out E> internal constructor(val value: V) : Result<V, E>() {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {

View File

@ -16,7 +16,7 @@ internal class AndTest {
@Test @Test
internal fun `andThen should return the result error if not ok`() { internal fun `andThen should return the result error if not ok`() {
val result = ok(20).andThen { ok(it + 43) }.andThen { error(AndError) } val result = ok(20).andThen { ok(it + 43) }.andThen { err(AndError) }
result as Error result as Error

View File

@ -15,7 +15,7 @@ internal class GetTest {
@Test @Test
internal fun `get should return null if not ok`() { internal fun `get should return null if not ok`() {
val value = error(GetError).get() val value = err(GetError).get()
assertThat(value, equalTo(null)) assertThat(value, equalTo(null))
} }
@ -27,7 +27,7 @@ internal class GetTest {
@Test @Test
internal fun `getOrElse should return default value if not ok`() { internal fun `getOrElse should return default value if not ok`() {
val value = error(GetError).getOrElse("default") val value = err(GetError).getOrElse("default")
assertThat(value, equalTo("default")) assertThat(value, equalTo("default"))
} }
} }

View File

@ -34,8 +34,8 @@ internal class IterableTest {
initial = 1, initial = 1,
operation = { a, b -> operation = { a, b ->
when (b) { when (b) {
(5 + 10) -> error(IterableError.IterableError1) (5 + 10) -> err(IterableError.IterableError1)
(5 + 10 + 15 + 20) -> error(IterableError.IterableError2) (5 + 10 + 15 + 20) -> err(IterableError.IterableError2)
else -> ok(a * b) else -> ok(a * b)
} }
} }
@ -65,8 +65,8 @@ internal class IterableTest {
initial = 38500, initial = 38500,
operation = { a, b -> operation = { a, b ->
when (b) { when (b) {
(((38500 / 40) / 20) / 10) -> error(IterableError.IterableError1) (((38500 / 40) / 20) / 10) -> err(IterableError.IterableError1)
((38500 / 40) / 20) -> error(IterableError.IterableError2) ((38500 / 40) / 20) -> err(IterableError.IterableError2)
else -> ok(b / a) else -> ok(b / a)
} }
} }
@ -96,9 +96,9 @@ internal class IterableTest {
val result = combine( val result = combine(
ok(20), ok(20),
ok(40), ok(40),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok(60), ok(60),
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok(80) ok(80)
) )
@ -112,9 +112,9 @@ internal class IterableTest {
val values = getAll( val values = getAll(
ok("hello"), ok("hello"),
ok("big"), ok("big"),
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok("wide"), ok("wide"),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok("world") ok("world")
) )
@ -128,15 +128,15 @@ internal class IterableTest {
@Test @Test
internal fun `getAllErrors should return all of the result errors`() { internal fun `getAllErrors should return all of the result errors`() {
val errors = getAllErrors( val errors = getAllErrors(
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok("haskell"), ok("haskell"),
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok("f#"), ok("f#"),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok("elm"), ok("elm"),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok("clojure"), ok("clojure"),
error(IterableError.IterableError2) err(IterableError.IterableError2)
) )
assertThat(errors.size, equalTo(5)) assertThat(errors.size, equalTo(5))
@ -150,15 +150,15 @@ internal class IterableTest {
@Test @Test
internal fun `partition should return a pair of all the result values and errors`() { internal fun `partition should return a pair of all the result values and errors`() {
val pairs = partition( val pairs = partition(
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok("haskell"), ok("haskell"),
error(IterableError.IterableError2), err(IterableError.IterableError2),
ok("f#"), ok("f#"),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok("elm"), ok("elm"),
error(IterableError.IterableError1), err(IterableError.IterableError1),
ok("clojure"), ok("clojure"),
error(IterableError.IterableError2) err(IterableError.IterableError2)
) )
val values = pairs.first val values = pairs.first

View File

@ -25,7 +25,7 @@ internal class MapTest {
@Test @Test
internal fun `map should return the result error if not ok`() { internal fun `map should return the result error if not ok`() {
val result = error(MapError.HelloError).map { "hello $it" } val result = err(MapError.HelloError).map { "hello $it" }
result as Error result as Error
@ -44,7 +44,7 @@ internal class MapTest {
.map { "$it me" } .map { "$it me" }
.andThen { .andThen {
when (it) { when (it) {
"let me" -> error(MapError.CustomError("$it $it")) "let me" -> err(MapError.CustomError("$it $it"))
else -> ok("$it get") else -> ok("$it get")
} }
} }
@ -67,7 +67,7 @@ internal class MapTest {
@Test @Test
internal fun `mapBoth should return the transformed result error if not ok`() { internal fun `mapBoth should return the transformed result error if not ok`() {
val error = error(MapError.CustomError("this")).mapBoth( val error = err(MapError.CustomError("this")).mapBoth(
success = { "$it charming" }, success = { "$it charming" },
failure = { "${it.reason} man" } failure = { "${it.reason} man" }
) )
@ -89,7 +89,7 @@ internal class MapTest {
@Test @Test
internal fun `mapEither should return the transformed result error if not ok`() { internal fun `mapEither should return the transformed result error if not ok`() {
val result = error("the reckless").mapEither( val result = err("the reckless").mapEither(
success = { "the wild youth" }, success = { "the wild youth" },
failure = { MapError.CustomError("the truth") } failure = { MapError.CustomError("the truth") }
) )

View File

@ -18,14 +18,14 @@ internal class OnTest {
@Test @Test
internal fun `onSuccess should not invoke the callback when result is not ok`() { internal fun `onSuccess should not invoke the callback when result is not ok`() {
val counter = Counter(200) val counter = Counter(200)
error(CounterError).onSuccess { counter.count -= 50 } err(CounterError).onSuccess { counter.count -= 50 }
assertThat(counter.count, equalTo(200)) assertThat(counter.count, equalTo(200))
} }
@Test @Test
internal fun `onFailure should invoke the callback when result is not ok`() { internal fun `onFailure should invoke the callback when result is not ok`() {
val counter = Counter(555) val counter = Counter(555)
error(CounterError).onFailure { counter.count += 100 } err(CounterError).onFailure { counter.count += 100 }
assertThat(counter.count, equalTo(655)) assertThat(counter.count, equalTo(655))
} }

View File

@ -15,7 +15,7 @@ internal class OrTest {
@Test @Test
internal fun `or should return the default value if not ok`() { internal fun `or should return the default value if not ok`() {
val error = error(OrError).or(5000).get() val error = err(OrError).or(5000).get()
assertThat(error, equalTo(5000)) assertThat(error, equalTo(5000))
} }
@ -27,7 +27,7 @@ internal class OrTest {
@Test @Test
internal fun `extract should return the transformed result error if not ok`() { internal fun `extract should return the transformed result error if not ok`() {
val error = error("hello").extract { "$it darkness" } val error = err("hello").extract { "$it darkness" }
assertThat(error, equalTo("hello darkness")) assertThat(error, equalTo("hello darkness"))
} }
} }