[swift-evolution] Testing enum cases with associated values

Tony Allevato tony.allevato at gmail.com
Wed Jan 18 12:15:52 CST 2017


FWIW, I'm not convinced that making enum values look like structs with
optional properties for the union of all their cases is a good idea. It's
certainly not something I would want added to all of my enums by default,
and it feels IMO like it's just an awkward hack around the fact that
pattern matching is a bit verbose in some scenarios because Swift doesn't
provide a case-expression to let you test/bind as part of a larger
expression.


On Wed, Jan 18, 2017 at 10:08 AM Andy Chou via swift-evolution <
swift-evolution at swift.org> wrote:

> That’s an interesting proposal. Here’s a link for reference:
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160926/027287.html
>
> The one thing is the proposed syntax doesn’t handle enum cases without
> payloads. I think that could be handled by an optional Void, so this
> comment in the original proposal:
>
> > Only enum cases with a payload can be used with this syntax (it would
> make no sens for cases without a payload).
>
> Would be changed to allow for empty payloads turning into Void?. For
> example:
>
> enum Result {
>     case success(Int)
>     case failure
> }
>
> let r: Result = foo()
>
> let x: Int? = r.success
> let y: Void? = r.failure
>
> assert(r.success == Optional(42))
> assert(r.failure == nil)
>
> I think it’s a reasonable compromise, though I still think it’s a bit
> awkward for the common case. Looks like this is being postponed for now, so
> we’ll have to live with the alternatives.
>
> Andy
>
>
> On Jan 18, 2017, at 12:20 AM, Anton Zhilin <antonyzhilin at gmail.com> wrote:
>
> AFAICS, Andy needs not default implementations of Equatable, but
> cases-as-optional-properties—this topic has also been discussed on the list.
>
> enum Result {
>     case success(Int)
>     case failure(String)
> }
>
> let r: Result = foo()
>
> let x: Int? = r.success
> let y: String? = r.failure
>
> assert(r.success == Optional(42))
> assert(r.failure == nil)
>
>>
>
> _______________________________________________
> 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/20170118/c44c2968/attachment.html>


More information about the swift-evolution mailing list