[swift-evolution] [proposal] Either in the Swift Standard Library

Andrew Bennett cacoyi at gmail.com
Tue Jan 26 03:52:03 CST 2016


It seems like the argument comes down to:
 * An either-like type should be named something meaningful for the context
it is used
 * There are a lot of cases where an either-like type is useful, there may
generalised functions that are often used on them
 * Is defining or re-defining an either type hard?
 * Is it too niche to be in the standard library

Personally I think defining an either type is not hard:


    enum Result<T,E: ErrorType> {

        case Value(T), Error(E)

    }

I think it may be too niche to be in the standard library, from the
"meaningful context" perspective.

Perhaps what we want is not an either type, but an easy to conform to
protocol that allows some reuse with Either-style types. Possibly with some
compiler magic to make it easy for enums to conform to it...

I've thrown an example protocol together here:
    https://github.com/therealbnut/Either


On Tue, Jan 26, 2016 at 6:59 PM, Developer via swift-evolution <
swift-evolution at swift.org> wrote:

> Because the shape is what matters to me most.  Whether the type is defined
> as
>
> enum Either<L, R> { //... }
> enum Result<T, E> { //... }
> enum Choice<This, That> { //... }
> enum XOR<L, R> { //... }
> enum V<L, R> { //... }
> enum These<L, R> { //... }
>
> the point is that the standard library offers a sum type that doesn't
> attempt to invade the space taken over by throws (as I say in the
> proposal).  If we are to encourage that, then a type named Result that has
> a lobe specifically dedicated to error handling, in some cases even
> constrained by ErrorType, is an invasion indeed.   The type needn't carry
> semantics already provided by the language, and in that case it must carry
> the most general of semantics: those implied by structure and reinforced by
> documentation.
>
> As for bias, I'll admit that it is insufficiently addressed in the
> proposal (however I do acknowledge this very question would arise).  We
> chose a left bias because of Rust's success and a lack of Haskell-style
> biased type application to deal with in type classes (not that we have any
> now anyway).  From a less technical perspective, the idea that an error
> should come first is counterintuitive.  It makes more sense to emphasize
> the successful case by placing it "first".
>
> I believe we disagree at such a level, outside of naming and what all that
> entails, because this seems like a Haskellism and Haskell's idea about
> error handling is opinionated to some and is made even more so by an
> implicit understanding required to interact with the rest of the
> ecosystem.  Either is not a fundamentally meaningless type in Haskell, or
> here, and it has a very precise interpretation computationally.  Trouble
> is, conveying that sentiment also inevitably leads to incredibly technical
> arguments that leaves novices with a bitter taste - who wants to learn
> about Profunctors to figure out why Either had a right-bias anyhow?  The
> proposal is aimed towards the Swift community, not the Haskell one.
>
> I request that you suspend any preconceptions and take this for what it
> is: A sum type, nothing more.  If we wished to cause damage to the
> community, this proposal would be about replacing throws entirely and
> forcing this change - or even a Result<T> variant of it - upon everybody.
> In the short term this will make a few more people sit up and think about
> when they need throws and when they need a sum.  In the long term it will
> enable a new class of typesafe programs and higher-order patterns to be
> written without dependence on a third party.
>
> So, that said, I'm open to a name change, but we must keep the generality
> of the type intact to a point.
>
> ~Robert Widmann
>
> 2016/01/26 2:30、Kevin Ballard <kevin at sb.org> のメッセージ:
>
> No, I take issue with the entire fundamental idea behind your proposal.
> You've explicitly defined this type purely by its shape, with no meaning
> whatsoever given to either variant, except insofar as the type is
> Left-biased (which only serves to be even more confusing; since there's no
> actual meaning assigned to Left/Right, what is the justification for adding
> the left-biasing?)
>
> A type like this in the standard library will have a significant
> detrimental effect on the entire Swift ecosystem. Swift makes it so easy to
> define types with semantic meaning, but this type is only useful if people
> actually use it, and every time someone uses this they'll be losing the
> semantic meaning that would have been carried by the type they'd have
> defined otherwise.
>
> Also, FWIW, every single one of your motivating examples would be
> significantly improved by the use of a Result<T> or Result<T,E> instead of
> Either.
>
> -Kevin Ballard
>
> On Jan 25, 2016, 11:21 PM -0800, Developer <devteam.codafi at gmail.com>,
> wrote:
>
> So you take issue with the name?  Have you seen our section of
> alternatives?
>
> ~Robert Widmann
>
> 2016/01/26 1:39、Kevin Ballard <kevin at sb.org> のメッセージ:
>
> Then we are fundamentally at odds, because I am categorically opposed to
> any solution that does *not* have specific meaning attached to the two
> variants. Either is an awful type that only serves to make APIs harder to
> understand, and literally every non-trivial usage of Either I’ve actually
> seen in practice, it has been used exactly the way Result<T> would
> naturally be defined (where “trivial” means usage in any kind of actual
> API, as opposed to sample code or scratch functions using Either to
> prototype something that, if turned into actual API, would replace the
> usage of Either with a more well-defined enum). And the existence of Either
> only serves to encourage people to use it, which produces an overall
> negative effect on the quality of APIs.
>
> -Kevin Ballard
>
> On Jan 25, 2016, at 10:27 PM, Developer <devteam.codafi at gmail.com> wrote:
>
> Are you opposed to the name or the semantics?
>
> I will not accept a revision that reduces the level of abstraction of the
> current proposal.  I will, however, accept name changes.  Result, though, I
> believe is out of the question.  It strongly implies a common but pointed
> set of semantics that discourage thinking of this type as data and more as
> an alternative to throws.  I do not wish to emphasize the error case, or
> the theoretical case, I wish to encourage the general case.  We must
> remember that despite Rust's success, they do not have to live alongside an
> exceptions mechanism like Either does.
>
> ~Robert Widmann
>
> 2016/01/26 0:55、Kevin Ballard via swift-evolution <
> swift-evolution at swift.org> のメッセージ:
>
> There absolutely is a cost. `Result<T>` has a rather intuitive meaning.
> `Either<T>` has *no intuitive meaning whatsoever*. It says absolutely
> *nothing* about what it means beyond the fact that there are two
> potential values. As a result, it is a largely useless type whose sole
> redeeming feature is it allows developers to avoid having to define their
> own enum, but in most cases that aren't covered by Result<T> you actually
> *want* to define your own enum so you can attach meaning to the value.
>
> If it's not obvious, I'm very strongly against having a generic Either
> type, but I do want a Result<T> or Result<T,E>.
>
> -Kevin Ballard
>
> On Fri, Jan 22, 2016, at 10:22 PM, Developer via swift-evolution wrote:
>
> My overwhelming concern, after having a conversation with Chris, is that
> implementing a Result<T> means we are strongly implying a particular
> semantics and use case when we could generalize and abstract for no cost
> but an extra generic parameter.  In F#, Core.Choice can be used to build a
> Validation or Result monad, but the converse is impossible.
>
> ~Robert Widmann
>
> 2016/01/23 1:05、Rob Mayoff via swift-evolution <swift-evolution at swift.org>
> のメッセージ:
>
> Just added a section of motivating examples to the Either proposal.  Ping
> me if you have any more that I missed ('cause I'm sure I did miss a lot).
>
>
> https://github.com/typelift/swift-evolution/blob/either-or/proposals/0024-either.md#motivating-examples
>
>
>
> Your motivating examples (including all the projects you linked except
> "Any many more") overwhelmingly use the Either (or similar type) to
> represent success/failure. I'm not sure there's a single example where the
> names Left and Right actually make sense in the problem domain. I'm not
> 100% sure about func alternate in Madness/Alternation.swift. It definitely
> uses Left/Right to mean Failure/Result, but I couldn't tell if it also uses
> them as something else. Which makes those names all the more maddening.
>
> I checked my company's largest Scala project, which is over 300,000 lines.
> We use Scala's Try/Success/Failure in dozens of places. We use
> Either/Left/Right once, in a thrown-together report-generating script,
> which would probably have been written in awk or perl if it didn't need to
> read binary log files. (The ability of IntelliJ to reliably find all uses
> of a class or method is not to be underestimated. Hint hint, team Xcode.)
>
> I think a Result/Success/Failure type is warranted, but I'm very skeptical
> about generic Either/Left/Right.
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> *_______________________________________________*
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160126/40f9489e/attachment.html>


More information about the swift-evolution mailing list