Compare the listing without considering its arguments

Let me make this clear, I have this listing:

enum Token {
    Number(v:Float);
    Identifier(v:String);
    TString(v:String);
    Var;
    Assign;
    Division;
    // and so on
}

I want to check if the value of a variable is an identifier, but this does not work:

if(tk == Token.Identifier) {

This allows me to compare values ​​if I pass arguments:

if(tk == Token.Identifier('test')) {

But this will only match if the identifier is a “test”, but I want to match any identifier.

+3
source share
3 answers
Type.enumConstructor(tk) == "Identifier"

Read the Type doc for more methods for listing.

+6
source

as an alternative:

static function isIdentifier(token : Token) return switch(token) { case Token.Identifier(_): true; default: false; }

Using "use", you can also:

if(tk.isIdentifier()) {
+4
source

:

tk.match(Token.Identifier(_));
+1

Source: https://habr.com/ru/post/1766038/


All Articles