Forum > Suggestions
[ resolved] Allow case to use const array elements and function results
Joanna from IRC:
Hello , I recently discovered that I’m not allowed to use values from a constant array in a case statement. Like this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- procedure correct (sender:tobject); const AR_CTRL_NAMES:ARRAY[0..1] OF String = ('MIN_CTRL','MAX_CTRL');beginCASE TControl(SENDER).Name OF // doesnt matter what type the control is AR_CTRL_NAMES[0]:MAXIMUM := MINIMUM; AR_CTRL_NAMES[1]:MINIMUM := MAXIMUM; OTHERWISE ShowMessage(name+' ERROR '); END; end; The error is constant and case types don’t match.
It will work for lone const strings by themselves of course but I need to keep things organized. The same restrictions seem to apply regardless of type in the array. Is there a reason this can’t work? Yes I know I can use if statement but that looks sloppy and will not scale well for many choices.
I also thought of using function String results in the case statement and that won’t work either.
440bx:
--- Quote from: Joanna on January 22, 2024, 11:35:22 pm ---Is there a reason this can’t work?
--- End quote ---
The reason is because a case statement is designed to compare against a constant and, in the code you posted, the array - being a typed constant - is actually a variable and the "name" field is also a variable, therefore a "case" statement will be "unhappy" about that situation.
you can easily emulate a variable against variable case statement by creating an inline scope. e.g,
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 1 to 1 do // the fake scope, a loop that executes only once (NOTE: can use a repeat ... until TRUE instead)begin if <somevar> = <someothervar> then begin DoSomething(); break; // the break is important end; if <somevar2> = <whoknowswhat> then begin DoTheOtherThing(); break(); end; { more cases here } // the "otherwise" case DoTheOtherwiseCase(); // a break is not needed for the emulated "otherwise" but, I recommend you code it just in case it becomes a case instead of the general "otherwise" break;end; // code outside the "case" here, which is where the above "break"(s) land
The above is just like a case without any of the limitations of a case. That said, the compiler will not optimize such a "case" statement into a jump table but, it can't do that anyway whenever variables are involved, therefore there is no loss.
HTH.
Just for fun: Delphi 2 (and likely newer versions) optimizes one of these fake scopes by removing the loop then the compiler issues an internal error because it cannot find the target of the "break" (because it removed the loop) <chuckle>
Lastly instead of using "for i := 1 to 1 do", it is a bit clearer to write "for SCOPE := SCOPE to SCOPE do" that should really clue the reader that the "for" isn't a loop but a scope (or repeat... until SCOPE = SCOPE;)
Joanna from IRC:
Thanks for the answer I didn’t think of as a static variable. Apparently the values in a constant array can be changed.
I wish that there could be a new reserved word for static variables besides const. It is very misleading. :(
440bx:
--- Quote from: Joanna on January 23, 2024, 01:19:01 am ---I wish that there could be a new reserved word for static variables besides const. It is very misleading. :(
--- End quote ---
Join the club but, the developers have made it clear they will _not_ implement a new reserved word for static variables.
Also, in addition to misleading, it's really hard to have a straight face when explaining to someone who is new to Pascal that some constants can be changed. That's when they usually say something to the effect of not being surprised the language has lost popularity (changeable constants... hmmm... just what the industry needed.)
RayoGlauco:
I think the job can get done by using 'else if' sentences:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---aName:=TControl(SENDER).Name;if aName = AR_CTRL_NAMES[0] then MAXIMUM := MINIMUMelse if aName = AR_CTRL_NAMES[1] then MINIMUM := MAXIMUMelse ShowMessage(name+' ERROR ');
Navigation
[0] Message Index
[#] Next page