GithubHelp home page GithubHelp logo

Comments (7)

michaelbull avatar michaelbull commented on June 1, 2024 1

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.

1gravity avatar 1gravity commented on June 1, 2024 1

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.

1gravity avatar 1gravity commented on June 1, 2024

Thanks for the explanations, you convinced me.

from kotlin-result.

michaelbull avatar michaelbull commented on June 1, 2024

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.

kolyneh avatar kolyneh commented on June 1, 2024

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.

michaelbull avatar michaelbull commented on June 1, 2024

@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.

kolyneh avatar kolyneh commented on June 1, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.