Monkey C: Logical OR and AND behave inconsistently/incorrectly for Number types
o The Environment:
Windows 10
Eclipse Oxygen (4.7.0)
Connect IQ 2.3.1
o A detailed description of the issue
In Monkey C, Logical OR and logical AND operators are not applied to Numbers in a consistent manner. I assume that the same issue exists for Longs, although I have not tested it.
OR:
In Monkey C, logical OR applied to two Numbers seems to behave like JavaScript.
In JS, X || Y returns X if X is truthy, Y otherwise.
e.g. 5 || 12 equals 5.
AND:
In Monkey C, logical AND applied to Numbers seems to behave like bitwise AND, which is not expected or consistent. In Monkey C, 5 && 12 equals 4
In JS, X && Y returns Y if X is truthy, X otherwise.
e.g. 5 && 12 equals 12.
o Steps to reproduce the issue
Build and run the following test code in the simulator (e.g. FR230, FR935). Observe the console output.
Actual output:
x = 5
y = 12
x | y = 13
x || y = 5
x & y = 4
x && y = 4
Expected output (javascript-style behaviour)
x = 5
y = 12
x | y = 13
x || y = 5
x & y = 4
x && y = 12
Expected output (C-style behaviour). (Provided to show that Monkey C's behaviour is consistent with neither javascript, C nor itself).
x = 5
y = 12
x | y = 13
x || y = 1
x & y = 4
x && y = 1
o Any applicable additional information
Related bug report: https://forums.garmin.com/forum/deve...and-long-types
As a side note, it seems inconsistent that logical operators can apply to Numbers stored in variables, but not constants. The latter produces a compilation error.
Also, it's a little disappointing that this kind of thing doesn't seem to be documented, either explicitly or through examples.
o A code sample that can reproduce the issue
Please replace square brackets with parens in the following code.
using Toybox.WatchUi as Ui;
class testdatafieldView extends Ui.SimpleDataField {
function initialize[] {
SimpleDataField.initialize[];
var x = 5;
var y = 12;
System.println["x = " + x];
System.println["y = " + y];
System.println["x | y = " + [x | y]]; // behaves as expected , prints 13
System.println["x || y = " + [x || y]]; // behaves like javascript, prints 5
System.println["x & y = " + [x & y]]; // behaves as expected, prints 4
System.println["x && y = " + [x && y]]; // behaves like bitwise AND, prints 4 [!] [javascript would print 12]
}
function compute[info] {
return 0.0;
}