Can you satisfy a static member with an enumeration case?

I was first trying out a blank number type with:

struct Test1 {}

extension Test1: AdditiveArithmetic {
    static let zero = Test1()
    //...
}

Then I wondered if I could the same thing with a single-state enum:

enumTest2 { case zero }

I tried adding AdditiveArithmetic conformance, but it turns out that Xcode 10.3 insisted that I add a static member to satisfy the zero static property. Of course, doing that creates an error that the property clashes with the case. But pulling back, it seems that it should work directly; .zero is the name of a value of type Test2, just like we can play around between cases and type-static names when making an OptionSet.

The version of Xcode 11 beta 6 crashes every time I try to use it, so I have no idea what its version of Swift does.

Using enum cases this way is not (yet) supported, it's tracked by SR-3170. There are a few questions around whether cases with associated values might be able to satisfy function requirements, but I think the main obstacle here is an implementation + proposal.

3 Likes
Terms of Service

Privacy Policy

Cookie Policy