Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How may I test the equivalency of enumeration cases with associated values in Swift 4

I would like to test the equivalency of a couple variables of enumeration types, like this:

enum AnEnumeration {
  case aSimpleCase
  case anotherSimpleCase
  case aMoreComplexCase(String)
}

let a1 = AnEnumeration.aSimpleCase
let b1 = AnEnumeration.aSimpleCase
a1 == b1 // Should be true.

let a2 = AnEnumeration.aSimpleCase
let b2 = AnEnumeration.anotherSimpleCase
a2 == b2 // Should be false.

let a3 = AnEnumeration.aMoreComplexCase("Hello")
let b3 = AnEnumeration.aMoreComplexCase("Hello")
a3 == b3 // Should be true.

let a4 = AnEnumeration.aMoreComplexCase("Hello")
let b4 = AnEnumeration.aMoreComplexCase("World")
a3 == b3 // Should be false.

Sadly, these all produce this errors like this one:

error: MyPlayground.playground:7:4: error: binary operator '==' cannot be applied to two 'AnEnumeration' operands
a1 == b1 // Should be true.
~~ ^  ~~

MyPlayground.playground:7:4: note: binary operator '==' cannot be synthesized for enums with associated values
a1 == b1 // Should be true.
~~ ^  ~~

Translation: If your enumeration uses associated values, you can't test it for equivalency.

Note: If .aMoreComplexCase (and the corresponding tests) were removed, then the code would work as expected.

It looks like in the past people have decided to use operator overloading to get around this: How to test equality of Swift enums with associated values. But now that we have Swift 4, I wonder if there is a better way? Or if there have been changes that make the linked solution invalid?

Thanks!

like image 912
MadEmperorYuri Avatar asked Oct 17 '17 03:10

MadEmperorYuri


People also ask

Can you give useful examples of enum associated values?

However, the key word here is “useful”, which means you need to provide an example that is even vaguely real world. For instance, you might describe a weather enum that lists sunny, windy, and rainy as cases, but has an associated value for cloudy so that you can store the cloud coverage.

What is associated value in enum?

In Swift enum, we learned how to define a data type that has a fixed set of related values. However, sometimes we may want to attach additional information to enum values. These additional information attached to enum values are called associated values.

What Swift keyword is used to define the values for an enumeration?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.

Can enum conform to Swift protocol?

Yes, enums can conform protocols. You can use Swift's own protocols or custom protocols. By using protocols with Enums you can add more capabilities.


1 Answers

The Swift proposal

  • SE-0185 Synthesizing Equatable and Hashable conformance

has been accepted and implemented in Swift 4.1 (Xcode 9.3):

... synthesize conformance to Equatable/Hashable if all of its members are Equatable/Hashable.

therefore it suffices to

... opt-in to automatic synthesis by declaring their type as Equatable or Hashable without implementing any of their requirements.

In your example – since String is Equatable – it will suffice to declare

enum AnEnumeration: Equatable {
  case aSimpleCase
  case anotherSimpleCase
  case aMoreComplexCase(String)
}

and the compiler will synthesize a suitable == operator.

like image 94
Martin R Avatar answered Sep 28 '22 02:09

Martin R