Should I live with compiler warnings about CONSTANT declarations?
Bill Lee
Posts: 5
I'm a relatively new Netlinx programmer, and keep getting warning messages when I try to compile code that has some CONSTANTs defined. It offends my sense of neatness, but it compiles OK. Should I just live with the warning messages? Is there a better way of defining these constants? Sample code was created and pasted into a new Netlinx template was:
(***********************************************************)
DEFINE_CONSTANT
CONSTANT HIGHSLONG = 2147483647
CONSTANT LOWSLONG = -2147483647
CONSTANT LOWSLONG_00 = -1
CONSTANT LOWSLONG_01 = -10
CONSTANT LOWSLONG_02 = -100
CONSTANT LOWSLONG_03 = -1000
CONSTANT LOWSLONG_04 = -10000
CONSTANT LOWSLONG_05 = -100000
CONSTANT LOWSLONG_06 = -1000000
CONSTANT LOWSLONG_07 = -10000000
CONSTANT LOWSLONG_08 = -100000000
CONSTANT LOWSLONG_09 = -1000000000
CONSTANT LOWSLONG_10 = -2147483640
CONSTANT SLONG HIGHSLONGA = 2147483647
CONSTANT SLONG LOWSLONGA = -2147483647
CONSTANT SLONG LOWSLONG_00A = -1
CONSTANT SLONG LOWSLONG_01A = -10
CONSTANT SLONG LOWSLONG_02A = -100
CONSTANT SLONG LOWSLONG_03A = -1000
CONSTANT SLONG LOWSLONG_04A = -10000
CONSTANT SLONG LOWSLONG_05A = -100000
CONSTANT SLONG LOWSLONG_06A = -1000000
CONSTANT SLONG LOWSLONG_07A = -10000000
CONSTANT SLONG LOWSLONG_08A = -100000000
CONSTANT SLONG LOWSLONG_09A = -1000000000
CONSTANT SLONG LOWSLONG_10A = -2147483640
CONSTANT PLACEHOLDER = 1
(***********************************************************)
Compiling it gives the following warnings:
Starting NetLinx Compile - Version[2.5.2.20] [01-06-2014 12:00:30]
C:\AMX-work\TestOfConstantsDeclarations.axs
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(29): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(30): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(38): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(44): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(45): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(46): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(47): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(48): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(49): C10571: Converting type [LONG] to [SLONG]
C:\AMX-work\TestOfConstantsDeclarations.axs - 0 error(s), 9 warning(s)
The warning messages seem inconsistent in where they say the error is, as there are supposed to be nine problems, but there are more than this number of CONSTANT declarations that are essentially identical.
Should I just ignore this type of message as a peculiarity of the Netlinx compiler?
Bill Lee
(***********************************************************)
DEFINE_CONSTANT
CONSTANT HIGHSLONG = 2147483647
CONSTANT LOWSLONG = -2147483647
CONSTANT LOWSLONG_00 = -1
CONSTANT LOWSLONG_01 = -10
CONSTANT LOWSLONG_02 = -100
CONSTANT LOWSLONG_03 = -1000
CONSTANT LOWSLONG_04 = -10000
CONSTANT LOWSLONG_05 = -100000
CONSTANT LOWSLONG_06 = -1000000
CONSTANT LOWSLONG_07 = -10000000
CONSTANT LOWSLONG_08 = -100000000
CONSTANT LOWSLONG_09 = -1000000000
CONSTANT LOWSLONG_10 = -2147483640
CONSTANT SLONG HIGHSLONGA = 2147483647
CONSTANT SLONG LOWSLONGA = -2147483647
CONSTANT SLONG LOWSLONG_00A = -1
CONSTANT SLONG LOWSLONG_01A = -10
CONSTANT SLONG LOWSLONG_02A = -100
CONSTANT SLONG LOWSLONG_03A = -1000
CONSTANT SLONG LOWSLONG_04A = -10000
CONSTANT SLONG LOWSLONG_05A = -100000
CONSTANT SLONG LOWSLONG_06A = -1000000
CONSTANT SLONG LOWSLONG_07A = -10000000
CONSTANT SLONG LOWSLONG_08A = -100000000
CONSTANT SLONG LOWSLONG_09A = -1000000000
CONSTANT SLONG LOWSLONG_10A = -2147483640
CONSTANT PLACEHOLDER = 1
(***********************************************************)
Compiling it gives the following warnings:
Starting NetLinx Compile - Version[2.5.2.20] [01-06-2014 12:00:30]
C:\AMX-work\TestOfConstantsDeclarations.axs
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(29): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(30): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(38): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(44): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(45): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(46): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(47): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(48): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(49): C10571: Converting type [LONG] to [SLONG]
C:\AMX-work\TestOfConstantsDeclarations.axs - 0 error(s), 9 warning(s)
The warning messages seem inconsistent in where they say the error is, as there are supposed to be nine problems, but there are more than this number of CONSTANT declarations that are essentially identical.
Should I just ignore this type of message as a peculiarity of the Netlinx compiler?
Bill Lee
0
Comments
No, you should not ;-) I personally never release any code with compiler warnings (although they sometimes are hard to understand or - more often - are missing).
I'm not sure about your problem but have you tried to compile it without the "CONSTANT" key word? Since your constant declarations are within the DEFINE_CONSTANT section it is not necessary to use the CONSTANT key word (this is meant to be used in the DEFINE_VARIABLE section). Also be aware that if you omit the type of a variable or constant declaration the compiler assumes the type integer for number values and char for others. So your first few statements are actually all integer declarations but since you initialize (some of) them with a negative value the compiler has to cast them to a slong value (hence the warning). But this doesn't explain the fact that there are only nine warnings.
Patrick
#DISABLE_WARNING 10571
I'm sure I will think of why that solves the problem at some point, but it doesn't make sense to me right now either.
Now, when it comes to the constant keyword. This is a modifier that can be used when defining global variables. It basically allows you to mark variables as immutable and keep all of your definitions in the define_variable section, thus removing the need for a define_constant section. The approach to use is your choice, however placing a constant modifier within a define_constant section serves no purpose.
What you do need to do however is specify a type if you want anything other than integer or character values (which, looking at your values, you do). The format is this:
So taking your LOWSLONG again as an example it could be defined as either or
P.S. disabling those compiler warning is like taking the battery out of your smoke alarm as your house is burning down because the beeping is stopping you from sleeping.
Thanks to the people who replied to my original posting.
OK, in theory I agree: Ignoring compiler warnings are like ignoring the warning lights on the dashboard when you drive around. There are some occasions when you do have to ignore the warning lights though - but the important thing to know is when to ignore the lights because they are mistakenly indicating a fault in the engine but the fault is really in the monitoring circuitry.
In the meantime I've been off programming, writing a program that keeps track of a club membership for 600 members. This code allows RFID swipe or passcode manual validation, records attendance, allows entry of competition scores and can print receipts to a thermal printer. I wouldn't say it was perfect code, but it certainly allowed me to practice writing, building and uploading code.
Just now I did some more testing of the compiler and found the following results:
(***********************************************************)
DEFINE_CONSTANT
SLONG ASLONG = 2147483648 // !!! too big for a SLONG
SLONG BSLONG = 2147483647 // largest possible SLONG
SLONG CSLONG = 2147483646 // OK
SLONG DSLONG = 1 // OK
SLONG ESLONG = 0 // OK
SLONG FSLONG = -1 // OK
SLONG GSLONG = -32767 // OK
SLONG HSLONG = -32768 // OK
SLONG ISLONG = -65535 // OK
SLONG JSLONG = -65536 // !!!
SLONG KSLONG = -2147483647 // !!! smallest possible SLONG
(***********************************************************)
When the compiler was run the lines above marked with !!! had the following warning message
WARNING: C:\AMX-work\TestProg.axs(<line number + 1>): C10571: Converting type [LONG] to [SLONG]
Of these warning messages, only the warning for "SLONG ASLONG = 2147483648" is truly a legitimate warning of an improper allocation.
OK, now what happens if you try to do things with variables:
(***********************************************************)
DEFINE_VARIABLE
SLONG fred
(***********************************************************)
DEFINE_START
fred = -65534 // OK
fred = -65535 // OK
fred = -65536 // !!!
(***********************************************************)
The same warning message "Converting type [LONG] to [SLONG]" is given for the allocation of a number that should be OK for allocation to a SLONG variable.
Based on the help from the posters to my original question, I've made the changes in my definitions of Constants.
My conclusions are that the Netlinx compiler Version[2.5.2.20] will throw a warning message if you try to allocate a constant number to a SLONG variable if that constant number is lower than -65535. This is almost certainly just one of the quirks of the compiler since it should otherwise be OK to do, as long as this negative number is > -2147483647. I can't see a nice way of working around this and if you need to do this then you would probably have to just live with this warning message in this case.
The reason I wanted a very small SLONG number was to use this as an indicator that a returned number was outside acceptable limits. Of course I think now that maybe I should use an extremely negative but recognisable and testable number (like -1000000) instead of a number ( -(2^32 - 1)) that might might be generated inside the program if a calculation rolls over 32 bits.
Thanks,
Bill Lee
It is unnecessary to declare the type of constants you are defining.
If you will define the constant without the SLONG type declaration, the compiler will automatically type the value appropriately. The following compiles without any warning.
DEFINE_CONSTANT
MYSLONG = -2147483647 // or whatever
I know that many programmers have seen these warnings as the Forum thread shows. But the developers of the compiler simply did not expect programmers to try to assign types to the constants. So, when the additional information is included, the compiler is throwing the warning because of that unexpected data. Since constants cannot change, there is no need for that declaration.
I think it's important to point out that, as Bob says, types for constants aren't expected and (except in this case) the compiler seems to completely ignore them and use them however seems most appropriate. For example:
Finally, after reading about this issue I decided to try out some more ways to assign SLONGs and found that there is in fact a bug in the compiler. See the attached code if you'd like to try it out yourself. I'll also paste it below: