Ticket Created
over 2 years ago

CIQQA-1312

int and logical &&

4.1.3 win/eclipse

var x = 2, y= 4;
if(x)         {LOG.println(true);} else {LOG.println(false);}
if(y)         {LOG.println(true);} else {LOG.println(false);}
if(x && y)    {LOG.println(true);} else {LOG.println(false);}

prints

true
true
false <--- bug

  •  Not that it matters, but this is the kind of thing that makes me not want to update my apps or create new ones. Very frustrating bugs and design issues which come up over and over again.

  • Not to state the obvious but, the problem isn't with if statements -- they actually work the way the doc in your screenshot says.

    The problem is with the unexpected and inconsistent behavior of && and ||, for numbers.

    Monkey C's && acts like C's &: A && B => A & B (with short circuiting, afaik)

    Monkey C's || acts like JavaScript's ||: A || B => A ? A : B (with short circuiting, afaik)

    The other ridiculous thing (related to the above) is this:

    var x1 = null;
    var y1 = 5;
    if (x1) { // this is fine
    }
    if (y1) { // this is fine
    }
    if (y1 || x1) { //this is fine
    }
    if (x1 && y1) { //this is fine
    }
    if (y1 && x1) { //this is a type error
    }
    if (x1 || y1) { //this is a type error
    }
  • ok - it's clear - can't count &&/||  not compile or throw exception but no miscounting. I write code and it doesn't run and don't know why as it's consistent with math.

    There is no place for interpretation in math at all - you can interpret the clouds in the sky :)

    (true &&/AND true) = true

    (NotZero &&/AND NotZero) = NotZero (1,3,45 1000 but not 0)

  • The other hilarious thing here is that an expression with literal integers and "||"/"&&" is not allowed. The following expressions will fail to compile, for example:

    1 && 2
    1 || 2

  • To be clear, you're assuming Monkey C implements "&&" and "||" similarly for numbers, which it doesn't (sadly).

    And javascript doesn't always return the last operand, it does what I described:

    A || B => A ? A : B

    (if A is "truthy", return A, otherwise return B. Javascript also implements short-circuit logic here, meaning that if A is returned, then B is never evaluated)

    e.g.

    Code:

            var x = 254;
            var y = 1;
            System.println("x || y: " + (x || y));
            System.println("y || x: " + (y || x));
            System.println("x | y: " + (x | y));
    Output:
    x || y: 254
    y || x: 1
    x | y: 255