[swift-evolution] Request: Ability to refer to an enum case in abstract (without its associated value)

Sandy Chapman schapman at lixar.com
Fri Dec 11 07:43:49 CST 2015


You can already do something like this as well. Sure it's a bit verbose,
but for the cases where you need to ignore the associated value you can do
a one-to-one mapping. You can then simply do a switch(enumVar.type()) { ...
}.

enum EnumType {

    enum EnumTypeType {
        case TypeA
        case TypeB
        case TypeC
        case TypeD
    }

    case TypeA(String)
    case TypeB(Bool)
    case TypeC()
    case TypeD(String)

    func type() -> EnumTypeType {
        switch(self) {
        case .TypeA(_):
            return .TypeA
        case .TypeB(_):
            return .TypeB
        case .TypeC():
            return .TypeC
        case TypeD(_):
            return .TypeD
        }
    }
}

*Sandy Chapman*
*Technical Team Lead*


*Lixar I.T. Inc.*T: 902.405.4443
F: 902.405.4435

schapman at lixar.com
www.lixar.com

[image: Description: cid:image003.gif at 01CB115A.001B0250]

On Fri, Dec 11, 2015 at 8:51 AM, Al Skipp via swift-evolution <
swift-evolution at swift.org> wrote:

> I don’t think this suggestion would work in practice. It’s the equivalent
> of trying to construct a struct value with non-optional properties, but
> without providing any init parameters, it would result in an invalid value.
> If your proposal were possible I think it would cause trouble with pattern
> matching enums too. In a switch statement that expects to bind to
> associated values, what happens when it receives an enum that should have
> associated values, but they are not present?
>
>
> On 11 Dec 2015, at 04:53, Evan Maloney <emaloney at gilt.com> wrote:
>
> You're referring to the ability to test whether an existing enum value is
> of a given case regardless of the contents of its associated values. In
> other words, *after* a value has been created, you can test for whether
> it is of a specific case while ignoring any associated values.
>
> I'm talking about the ability to create a value that represents the
> generic concept of an enum case that has associated values, but without
> actually specifying any associated values.
>
> In Swift right now, you can programmatically represent an enum case that
> has associated values *only by first supplying those associated values*.
>
>
>
> _______________________________________________
> 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/20151211/8129d62c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 1631 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/8129d62c/attachment.jpg>


More information about the swift-evolution mailing list