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.
You can use the getClass() method to determine the class of an object. Also, if you want to check if an object implements an Interface or Class, you can use the instanceof keyword. That's it about checking the datatype of an object in Groovy.
Behaviour of == In Java == means equality of primitive types or identity for objects. In Groovy == translates to a. compareTo(b)==0, if they are Comparable, and a. equals(b) otherwise.
Class GString Represents a String which contains embedded values such as "hello there ${user} how are you?" which can be evaluated lazily.
== is symmetric in groovy. While that table is super handy, it's a bit misleading as == isn't actually on it.
Instanceof tests should work:
assert "fasd" instanceof String assert "${this}" instanceof GString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With