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

Austin Zheng austinzheng at gmail.com
Sat Jan 23 00:34:16 CST 2016


There have been discussions about generic typealiases/newtypes on this list before. If getting something in for Swift 2 isn't a must-have, I wonder if we could leverage those features to make both camps happy. Being able to partially apply a generic type to form a new generic type would allow us to have something like:

enum Either<T, U> {
  case Left<T>, Right<U>

typealias Result<GoodType> = Either< GoodType, ErrorType>

Of course, the above raises the question as to whether an 'Either' that just happens to have the right type be an ErrorType should be interchangeable with the corresponding 'Result', etc.

However, the above proposal might be infeasible from a theoretical or implementation perspective; if that's the case let's not get sidetracked :).


> On Jan 22, 2016, at 10:22 PM, Developer via swift-evolution <swift-evolution at swift.org> 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 <mailto: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 <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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <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/20160122/ee09c8db/attachment.html>

More information about the swift-evolution mailing list