I'm learning groovy to work on smartthings and found a relatively common command among the various examples and existing code (see below).
Reading the function of the && operator I would think the "&& cmd.previousMeterValue" is superfluous. Or is there some code shortcut I'm missing?
Thanks John
if (cmd.previousMeterValue && cmd.previousMeterValue != cmd.meterValue) {
do something
}
Not knowing what type
previousMeterValue
has, this answer is somewhat generic.Groovy follows common operator precedence, i.e.
!=
is evaluated before&&
.To show it explicitly, the full expression is the same as:
cmd.previousMeterValue
is testing the value for the Groovy-Truth.Depending on value type, the following might be applicable:
So if the value is
null
or0
, the expression isfalse
.If the first part of the expression evaluated to
false
, then the second part is skipped.If the first part of the expression evaluated to
true
, thencmd.previousMeterValue != cmd.meterValue
is evaluated, using the following rule:So if value is a number object, then it is evaluated as:
This means that
BigDecimal
values are compared by value, ignoring specific scale.