Skip to content

Commit

Permalink
add equals and hashCode, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Nov 14, 2023
1 parent 2731590 commit c1a76e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
- Kotlin 1.9.0

### 1.5.3
- Kotlin 1.9.10
- Kotlin 1.9.10

### 1.5.4
- Add `transform()` function to map results without nesting
- Add `mapCatching()`
- Implement `equals()` and `hashCode()`
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kotlin.native.binary.freezing=disabled
kotlin.mpp.stability.nowarn=true
kotlin.native.ignoreDisabledTargets=true

artifactVersion = 1.5.3
artifactVersion = 1.5.4
19 changes: 15 additions & 4 deletions src/commonMain/kotlin/at/asitplus/KmmResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ class KmmResult<T> private constructor(
getOrNull()?.let { success(block(it)) } ?: this as KmmResult<R>

/**
* Transforms this KmmResult into a KmmResult of different success type according to `block` and leaves the failure case untouched
* Avoids nested KmmResults
* Transforms this KmmResult into a KmmResult of different success type according to `block` and leaves the
* failure case untouched. Avoids nested KmmResults
*/
@Suppress("UNCHECKED_CAST")
inline fun <R> transform(block: (T) -> KmmResult<R>): KmmResult<R> =
getOrNull()?.let { block(it) } ?: this as KmmResult<R>


/**
* Returns the encapsulated result of the given [block] function applied to the encapsulated value
* if this instance represents [success][KmmResult.isSuccess] or the
Expand All @@ -105,7 +104,6 @@ class KmmResult<T> private constructor(
@Suppress("UNCHECKED_CAST")
inline fun <R> mapCatching(block: (T) -> R): KmmResult<R> = unwrap().mapCatching { block(it) }.wrap()


/**
* Transforms this KmmResult's failure-case according to `block` and leaves the success case untouched
* (type erasure FTW!)
Expand Down Expand Up @@ -155,6 +153,19 @@ class KmmResult<T> private constructor(
exName?.let { ")" }
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false

other as KmmResult<*>

return delegate == other.delegate
}

override fun hashCode(): Int {
return delegate.hashCode()
}

@OptIn(ExperimentalObjCRefinement::class)
companion object {
@HiddenFromObjC
Expand Down

0 comments on commit c1a76e9

Please sign in to comment.