While building a parser, I encountered a problem I don't know how to solve the idiomatic way. So I have a stream of Token, comming from a JSON file, and I wanted to automate the process of checking if the current token is of a given type, advance the pointer and return true if so, just return false otherwise. Troubles start when I deal with tokens carrying extra values :
This code works, but I don't really like the idea of constructing some random values to pass to the Parser::matches function. In this particular case, I just want to know if the current token is a Long or not, so I don't care what value it stores, but I can't just pass Token::Long.
I will have other tokens that carry two String for instance, so I'm not really happy with this solution. Maybe I could declare constants for those special cases, but is there another approach I'm not aware of?
In my parser the Token enum has type parameters for the extra data in the tokens, and the syntax depends only on the variant kind. To represent the kind I just plug () to all the type parameters. I did it this way mostly for testing purposes, so I'm not sure it's appropriate in your situation, though.
strum aside, the “vanilla”/canonical way to do this would be adding methods to Token indicating whether a given instance is a certain variant, something like: