A nasty Web IDE compiler "bug"

For the API function “Particle.connected()”: if you leave off the parens there is NO compile error message! I wasted a day of my life over this. Possibly this applies to other Particle.whatever functions.

Not exactly a compiler but.
It’s just how static class functions are treated.
You can test that with this

class someClass {
    static int a;
    static int test() { return ++a; }

While this can pose a “nasty” trap, there are similar traps that can cause a lot of headache (e.g. if (someVar = 10) instead of if (someVar == 10)) but won’t warrant a compiler error.
While some syntax is perfectly fine it often doesn’t make sense and that’s where compiler warnings would help.

As it happens, your case that’s what the compiler produced

__test.ino:11:19: warning: statement is a reference, not call, to function 'Particle.CloudClass::connect' [-Waddress]
__test.ino:11:19: warning: statement has no effect [-Wunused-value]

However, if you don’t have any real errors in your code you’ll never get to see them - but that is some undesirable behaviour of the Web IDE.

My usual take such things - when I for the life of me can’t find the reason for some strange behaviour - I deliberately add some syntax error (typically a stray } at the end of my code) to provoke an error and then check the warnings.


that is my worse trap of them all! I hate it when I fall in that one :skull:

@gusgonnet, one way to reduce the risk is to adopt this syntax instead

  if (10 == someVar); // that will build fine
  if (10 = someVar);  // this will throw a compile time error 

(it looks :stuck_out_tongue_closed_eyes: but prevents some :man_facepalming: :roll_eyes: )

1 Like

oh, I like that option. Thanks!

1 Like