groovytypes

How to check if an instance is of type String or GString in Groovy


I would like to learn what the robust way of checking, if a variable is of type string or gstring, is. I suppose it is different than in pure Java:

def var = someFunc();

if (var instanceof String || var instanceof GString) {
   String str = var;
}

I ask, because I do not want to be surprised that I have missed a Groovy specific feature that causes a hard to debug bug.


Solution

  • Instanceof tests should work:

    assert "fasd" instanceof String
    assert "${this}" instanceof GString