Similarity of enums with associated values

If you have read this previous post you have seen how to compare two enums with associated values for equality. The equality included the associated values as well which is only fair. But sometimes you are not interested in the associated values but only want to know, if the two enums are of the same case regardless of the possibly associated values; I call this in the following if they are „similar“.

OK, that should follow the same pattern we have used in the equality comparison. Let’s give it a try:

This method uses the same nested switch blocks as the comparison method but ignores all associated values. Now we can write something like this:

OK, OK. There is also a shorter version of the function with a switch on both sides simultaneously:

Cool, isn’t it? Here is the updated playground:


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.