[swift-evolution] [proposal] Either in the Swift Standard Library
dabrahams at apple.com
Wed Jan 27 01:25:34 CST 2016
on Mon Jan 25 2016, Chris Lattner <swift-evolution at swift.org> wrote:
>> On Jan 25, 2016, at 10:27 PM, Developer via swift-evolution
>> <swift-evolution at swift.org> 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.
> Here is my concern: Swift enums should be good enough that we don’t
> need an Either type. If defining your own custom enum is hard or bad,
> then we should fix that.
> There are a number of concepts floating around that would make enums
> better in various ways. One specific one would be to synthesize
> optional accessors that line up with enum cases.
> What will the advantage of Either be once enums get even more fully baked?
There's one thing something like Either does that "defining an enum
type" doesn't: you can use one in a generic signature with dependent
argument types, e.g.
func f<T,U>(_: Bool, x: T, y: U) -> SomethingLikeEither<T,U>
*Yes* you can always use a generic enum to define SomethingLikeEither,
but then each user that wants to do this has a hoop to jump through, and
the community doesn't have a common type that interoperates everywhere
we need something like Either. Imagine that we didn't have tuples in
the language and we didn't define a generic "Cons" struct in the
standard library for people to build their own tuples out of.
Now, I don't think any of this is a big enough cost to make it worth
putting SomethingLikeEither (with no implied semantics attached) in the
standard library, and product types are much more generally useful than
sum types are, so I think it's great that we have tuples. But to be
totally fair, we have to acknowledge that something like Either has
value, even after Enums become easier to use.
More information about the swift-evolution