Hello,
Of all my rules I have 1 warning problem that pop up on VS Code.
The rule runs fine but I don’t like having a warning
The warning is:
Bounds mismatch: The type arguments <Item, State> are not a valid substitute for the bounded type parameters <T, C extends Comparable<? super C>> of the method sortBy(Iterable, Function1<? super T, C>)
and the rules code is:
rule "Battery Monitor"
when Time cron "0 30 9 * * ?"
then
if (! Batteries.allMembers.filter([state < lowBatteryThreshold]).empty) {
val report = Batteries.allMembers.filter([state instanceof Number]).sortBy([state]).map[name + ": " + state.format("%d%%")].join("\n")
val message = "Battery levels:\n\n" + report + "\n\nRegards,\n\nOur House"
sendMail("xxxxxxxx@gmail.com", "Low battery alert", message)
}
end
This rule was pulled out straight out of example rules from the OH1 wiki.
Syntax checking and reporting has become much more strict in OH 2.2 compared to 1.x so there is bound to be a lot of similar warnings when grabbing examples from the 1.x wiki.
I assume that all members of Batteries are Number Items?
Try the following (note, most of your parens are not needed and since you don’t use the parens around the map I’ll remove them everywhere else they are not needed:
val report = Batteries.allMembers.filter[bat | bat.state instanceof Number].sortBy[state as Number].map[name + ": " + state.format("%d%%")].join("\n")
The major change is I define an argument to be passed to the lambda (which probably isn’t strictly required but it isn’t clear to me what is causing the error) and I cast the state in the sortBy to Number so we can be sure it will know how to sort it.
Keep in mind this is a warning, not an error. The code probably works as written. The warning itself, as arcane as it appears, is primarily complaining about the types passed to sortBy so my hope is by casting the state to a Number it will clear up the warning.