Comments (7)
One issue I have with it is that the IDE usually picks kotlin.Result as a default for the import
You can configure IntelliJ to exclude kotlin.Result
from auto-importing, as shown here.
Using Either would be closer to the standard for what Result represents (not just closer, exactly spot on ;-)
I disagree. Result is very strict in its definition, it specifically represents a success/failure of an operation, with the success on the left and the failure on the right. Either not an accurate representation whatsoever of this, it's more generic and is actually misleading.
With an Either
type you are trying to add an "OR" to the type system, to say it's returning a String
OR an Int
for example, but this isn't really what you end up with as the Either<String, Int>
. An Either type is not commutative or symmetric, i.e. Either<Int, String>
does not equal Either<String, Int>
. This also then introduces the problem of choosing a "biased side" for what you consider the success or the error (whether it's left or right), which varies across different functional languages.
Rich Hickey (author of Closure) has a very in-depth explanation of why Either
is a misnomer.
If you truly wish to use Either, you can import com.github.michaelbull.result.Result as Either
from kotlin-result.
I agree, I'm looking forward to having union types in Kotlin as well. So do a lot of other developers, #1 feature requested: https://blog.jetbrains.com/kotlin/2021/12/kotlin-features-survey-2021-results/.
from kotlin-result.
Thanks for the explanations, you convinced me.
from kotlin-result.
You're very welcome - thanks for the feedback on your usage of the library, it's great to hear. I've been around the Either/Result loop a few times, I think the true best way to solve it is on a type level with union types that languages like TypeScript have, e.g. myFunction(): String | Int
which returns a String
or an Int
within the type system without resorting to monads.
from kotlin-result.
Hi @michaelbull, I have configured IntelliJ to exclude kotlin.Result
from auto-importing, but it's not working. 😂
I think it's better if we change Result
to an interface, then add a typealias
definition for it:
...
- public sealed class Result<out V, out E> {
+ public sealed interface Result<out V, out E> {
...
+ // IResult for example because it is an interface, other names would be ok.
+ typealias IResult<K, V> = Result<K, V>
And then we can use IResult
to avoid conflict with kotlin.Result
.
from kotlin-result.
@kolyneh The docs[1] don't seem to suggest that the base Result would need to be an interface instead of a sealed class. You should be able to just declare typealias IResult<K, V> = com.github.michaelbull.result.Result
in your project and work around the problem you're hitting with IntelliJ. I can't say I'm hitting the same problem though, I've excluded it and it never suggests the stdlib one.
[1] https://kotlinlang.org/docs/type-aliases.html
from kotlin-result.
@michaelbull Yah, there is no need to use a sealed interface instead.
Just an example to add an alias for Result
.
Thanks!
from kotlin-result.
Related Issues (20)
- Kotlin bugs with value classes HOT 4
- How to use the new inlined Result in non-Kotlin code? HOT 9
- Issues with @JvmStatic code HOT 4
- Migrate to new kotlinx-coroutines-test API
- iosSimulatorArm64 and macOsArm64 support HOT 22
- Assertion helpers for tests HOT 3
- Feature request: mingwX86 target HOT 2
- Extensions for Kotlin Flow? HOT 7
- About component1() and 2 HOT 2
- Best practices for integrating with Flow<T> HOT 1
- ensureNotNull-alike binding scope extensions HOT 15
- e: Could not load module <Error module> HOT 6
- Recovery extension HOT 3
- Consider using consistent "error" naming over "failure"? HOT 2
- Consider adding zipOrAccumulate? HOT 13
- Documentation of order of elements in Iterable.kt HOT 2
- Discuss: Use coroutines instead of exceptions for binding HOT 2
- `orElse` for Error Type Transformation HOT 2
- Make BindException public HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-result.