[swift-evolution] Evaluating the case of an enum with associated values as a bool
Djura Retired Hunter
retired.hunter.djura at gmail.com
Fri Dec 22 07:16:16 CST 2017
Many styles of programming can take advantage of if/else and switch/case being actual expressions (actually, these are all special cases of the very general concept of "folding"). We don't have this in Swift, and I have occasion to be bothered by this almost on a daily basis in my work, especially when I try to be "concise", something for which Swift should be champion.
The best possible outcome would be for Swift to have pattern matching as a proper evaluated expression, but I would accept a new operator that takes advantage of some custom compiler magic, because the use cases are so many and the convenience would be so great.
As Colin Barret said, this is a very popular thing in many modern languages.
Elviro
> Il giorno 20 dic 2017, alle ore 19:32, Colin Barrett via swift-evolution <swift-evolution at swift.org> ha scritto:
>
> This would be easily solved if pattern matching was available as an expression, such as in Haskell, OCaml / Standard ML, and Scala / Kotlin. :-)
>
>> On Dec 20, 2017, at 11:44 AM, Ethan Diamond via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Hello everyone,
>>
>> One major pain point I've run into with Swift is the inability to evaluate the case of an enum that has associated values in a way that just returns a bool. We've been given the ability in a switch statement:
>>
>> enum Enum {
>> case a(param: String)
>> case b(param: String)
>> }
>>
>> let enumeration: Enum = a(param: "Hi")
>> switch enumeration {
>> case a:
>> // Do something
>> case b:
>> // Do something
>> }
>>
>> We'e been given the ability in the context of an if statement:
>>
>> enum Enum {
>> case a(param: String)
>> case b(param: String)
>> }
>>
>> let enumeration: Enum = a(param: "Hi")
>>
>> if case .a = enumeration {
>> // Do something
>> }
>>
>> But without a basic was of getting a bool for if an enum is a given case, here's a list of things I can't do:
>>
>> Where statements:
>>
>> enum Enum {
>> case a(param: Enum2)
>> case b(param: Enum2)
>> }
>>
>> enum Enum2 {
>> case c(param: String)
>> case d(param: String)
>> }
>>
>> let enumeration: Enum = a(param: "Hi")
>> switch enumeration {
>> case a(let inner) where [INNER CASE IS .c]
>> }
>>
>> ---------
>>
>> Filter an array for a certain case:
>>
>> Expertly explained by Erica Sadun here: http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/ <http://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/>
>>
>> ---------
>>
>> Nicely set a UIButton to hidden if an enum is a certain case:
>>
>> enum State {
>> case `default`
>> case searching(results: [Result])
>> }
>>
>> myButton.isHidden = [STATE IS .searching]
>>
>> ---------
>>
>> I've run into this issue a ton of times because I tend to represent my views a State enums. I haven't seen anything on the board for plans for solving this issue, thought. Has there been any discussion about addressing it? Ideally I'd be able to do this:
>>
>> enum Enum {
>> case a(param: String)
>> case b(param: String)
>> }
>>
>> let enumeration: Enum = a(param: "Hi")
>>
>> case .a = enumeration // Bool
>> case .a(let param) = enumeration // Bool, assigns "Hi" to "param"
>>
>> Thanks!
>> Ethan
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto: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/20171222/76cc4a01/attachment.html>
More information about the swift-evolution
mailing list