Forum > General

Constant and CASE Types do Not Match

<< < (2/3) > >>

440bx:

--- Quote from: local-vision on July 11, 2024, 10:42:27 pm ---Might that be (part of) the reason?

--- End quote ---
The reason for requiring constants and forbidding variables is because of the way the case statement is usually implemented in assembly.  The compiler tries, whenever possible/efficient, to create a jump table to each case.  A jump table can only be created if the case values are constants (enumerable.)

That said, in the case of using string constants, the compiler cannot generate a jump table as string constants are not enumerable therefore cannot be used to produce a jump table.

HTH.

Khrys:

--- Quote from: local-vision on July 11, 2024, 10:42:27 pm ---[...] the const will always be fixed at its design time value and not change during run time.

--- End quote ---
Well, about that... :-[


--- 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";}};} ---const  FOOBAR: String = 'Hello'; procedure Test();  FOOBAR := 'Goodbye';  // Compiles just fine!  WriteLn(FOOBAR);      // Output: 'Goodbye'end;
If the  {$J+}  switch is on (which it is by default in  {$mode objfpc}), then typed constants can be written to. They behave exactly like (static) variables with the difference that they must have an initial value at declaration.
This only applies to constants with an explicit type annotation, though; untyped constants truly are immutable.

local-vision:

--- Quote from: Khrys on July 12, 2024, 07:43:44 am ---

--- 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";}};} ---const  FOOBAR: String = 'Hello'; procedure Test();  FOOBAR := 'Goodbye';  // Compiles just fine!  WriteLn(FOOBAR);      // Output: 'Goodbye'end;

--- End quote ---

That is so strange. Did not know you could even do that. Or maybe I did but forgot long ago.

I can imagine that as defined as a String it could represent anything. If the constant were defined as a Real then it could only represent numeric values.

But without the Type the const is immutable.

440bx that is what was meant, not "enumerable" but "immutable" correct?

In any case, good point.

440bx:

--- Quote from: local-vision on July 12, 2024, 10:07:40 am ---440bx that is what was meant, not "enumerable" but "immutable" correct?

--- End quote ---
with the exception of strings, it must be both, immutable (IOW, constant) and enumerable which means,(have a predictable sequence which ordinals have but strings don't have, e.g, what comes after "ab" ? ... is it "ac" or "abc" or something else ?... they cannot be enumerated but, what comes after the numeral 3, it better be 4. 

The enumerability is what gives the compiler the ability to produce a jump table.

MarkMLl:
The mutable consts can be useful to retain state inside functions, where lifetime<>scope (i.e. unlike "traditional" ALGOL/Pascal). But I really wish Borland hadn't reused "const" like that but instead had introduced some sort of static qualifier.

MarkMLl

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version