0

I have an enum where most values are just labels/constants, and one value wraps a string.

I want to compare two variables of this enum type, but == does not compile. What's the proper way here?

The structure of the enum type is roughly like this:

enum Token {
    case plain(Substring)
    case parenOpen
    case parenClose
    case curlyOpen
    case curlyClose
    case pipe
    case colon
    case eof
}

I want to iterate tokens until I hit a certain token, so I have a function that basically takes an endToken parameter. The idea is if I see a parenOpen token I want to process tokens until I see parenClose, and if I see curlyOpen I want to process tokens until I see curlyClose, but this is a variable and depends on what I saw earlier, so I need to check against a variable.

How can I achieve this?

EDIT:

Someone suggested this is a duplicate of another question about matching the insides of the pattern. It is not the same. I want to ignore the inside of the value and just match against the value itself!

David Moles
  • 48,006
  • 27
  • 136
  • 235
hasen
  • 161,647
  • 65
  • 194
  • 231
  • @kiwisip not really the same question. – hasen Mar 10 '18 at 08:57
  • 1
    What is different in your question? If you want to compare two variables of this enum type with `==` you need exactly what is answered in above question. And you have an associated value in `case plain(Substring)`. – kiwisip Mar 10 '18 at 09:00
  • 2
    As of Swift 4.1 (currently in beta) it suffices that you declare `enum Token: Equatable { ... }` and the compile will synthesize the `==` operator automatically, compare https://stackoverflow.com/questions/46782139/how-may-i-test-the-equivalency-of-enumeration-cases-with-associated-values-in-sw. – Martin R Mar 10 '18 at 09:07
  • @hasen: Finding a suitable duplicate is "busywork" as well, and it is not yet clear to me why the above-linked Q&As don't answer your question. In order to check something like `while token != delimiter { ... }` you need a `==` operator for the enum type, and up to Swift 4.0 you have to implement that yourself. – Martin R Mar 10 '18 at 09:55
  • 1
    I would vote to close this as a duplice: the answer in the linked thread [is easily adapted to only comparing cases](https://gist.github.com/dfrib/96f19d0f620ebf9b93b261623b5b56c6) (and not the associated value): simply return `true` rather that `a == b` in the body of the `case`. – dfrib Mar 10 '18 at 10:21
  • Well, the link from @MartinR does answer this question. The other link by kiwisip does not. – hasen Mar 10 '18 at 14:52
  • 1
    I would say the link from kiwisip _does_ indeed answer the question; MartinR's link applies for Swift 4.1 which is still in beta. Did you look [at the gist I linked in my previous comment](https://gist.github.com/dfrib/96f19d0f620ebf9b93b261623b5b56c6)? There I make use of the technique from the thread linked to by kiwisip, but to equality test only cases and not assoc. values. Anyway, I dupe-closed this question and included both kiwisip's (pre Swift 4.1) and MartinR's (from Swift 4.1 and onwards) links as dupe targets. Do you need additional support to adapt the linked threads to you needs? – dfrib Mar 10 '18 at 17:31
  • We’re all trying to help you out here but you seem set on complaining rather than willing to utilize our help (and time spent trying to help). I’ve twiced mentioned a link to a gist that should make it clear how to utilize the linked dupe target, and even asked if you need additional support making use of the information we’ve provided you. 3rd try: did you have a look at the gist link? I generally try not feed trolls, but seeing as you’re a high-rep user: you should know the usefulness of linking questions via dupe targeting; this doesn’t mean there’s anything wrong with the question itself. – dfrib Mar 11 '18 at 09:48
  • @dfri If you are trying to help you could've simply written an answer, instead of a comment. Now, for my rant: SO is supposed to be a place where people find straight forward answers to their specific questions. Getting told to look at another question, even when it's different, and then figure out how to adapt it to your specific problem, does not help in making SO a good place for finding quick solutions to common coding problems. It's not a forum. Anyway, I ended up solving my problem by restructuring my data so that the enum has no associated values. – hasen Mar 11 '18 at 12:49

0 Answers0