[swift-evolution] Testing enum cases with associated values
Andy Chou
acchou2 at gmail.com
Wed Jan 18 12:56:56 CST 2017
I agree it’s overkill for what I’m really looking for, which is a simple way to get a Bool result from testing an enum against a specific case, without looking at the associated values.
Andy
> On Jan 18, 2017, at 10:15 AM, Tony Allevato <tony.allevato at gmail.com> wrote:
>
> 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 <mailto: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 <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 <mailto: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 <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <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/7a19a626/attachment.html>
More information about the swift-evolution
mailing list