Recent

Author Topic: Why are constants not constant - or not known...?  (Read 1091 times)

Birger52

  • Full Member
  • ***
  • Posts: 173
Why are constants not constant - or not known...?
« on: May 23, 2020, 12:05:57 pm »
In TForm1 I define
Code: Pascal  [Select][+][-]
  1.   private const
  2.     crMove = TCursor(1);
  3.     crCopy = TCursor(2);
  4.     crExch = TCursor(3);
  5.     dtNone : byte = 0;
  6.     dtMove : byte = 1;
  7.     dtCopy : byte = 2;
  8.     dtExch : byte = 4;
  9.  
Deciding hwat cursor to use
Code: Pascal  [Select][+][-]
  1. function TForm1.GetDragCursor(dtTyp:byte) : TCursor;
  2. var
  3.   doCopy, doExch : boolean;
  4.   drCur : TCursor;
  5. begin
  6.   doCopy := (ssCtrl in GetKeyShiftState);
  7.   doExch := (ssAlt in GetKeyShiftState);
  8.   if doCopy and doExch then dtTyp := dtNone
  9.   else if doCopy then dtTyp := dtTyp and dtCopy
  10.   else if doExch then dtTyp := dtTyp and dtExch
  11.   else dtTyp := dtTyp and dtMove;
  12.   case dtTyp of
  13.     dtMove : drCur := crMove;
  14.     dtCopy : drCur := crCopy;
  15.     dtExch : drCur := crExch;
  16.     else drCur := crNoDrop;
  17.   end;
  18.   GetDragCursor := drCur;
  19. end;
gives errors:
Compile Project, Target: DropTest.exe: Exit code 1, Errors: 5
droptestform.pas(151,12) Error: Constant Expression expected // This is : after dtMove case..
droptestform.pas(152,12) Error: Constant Expression expected // This is : after dtCopy case..
droptestform.pas(152,12) Error: duplicate case label
droptestform.pas(153,12) Error: Constant Expression expected // This is : after dtExch case..
droptestform.pas(153,12) Error: duplicate case label
(Line 151 => 13)
Apparently, the compiler doesn't know defined constants...?

PascalDragon

  • Hero Member
  • *****
  • Posts: 1523
  • Compiler Developer
Re: Why are constants not constant - or not known...?
« Reply #1 on: May 23, 2020, 12:50:30 pm »
In TForm1 I define
Code: Pascal  [Select][+][-]
  1.   private const
  2.     crMove = TCursor(1);
  3.     crCopy = TCursor(2);
  4.     crExch = TCursor(3);
  5.     dtNone : byte = 0;
  6.     dtMove : byte = 1;
  7.     dtCopy : byte = 2;
  8.     dtExch : byte = 4;
  9.  

For use in case-statements you need to declare untyped constants:

Code: Pascal  [Select][+][-]
  1.   private const
  2.     crMove = TCursor(1);
  3.     crCopy = TCursor(2);
  4.     crExch = TCursor(3);
  5.     dtNone = 0;
  6.     dtMove = 1;
  7.     dtCopy = 2;
  8.     dtExch = 4;
  9.  

You can enforce a specific type by declaring them as e.g. Byte(0) like you did for the cursors.

Birger52

  • Full Member
  • ***
  • Posts: 173
Re: Why are constants not constant - or not known...?
« Reply #2 on: May 23, 2020, 02:09:35 pm »
Thx a mill!
 :D

MarkMLl

  • Hero Member
  • *****
  • Posts: 870
Re: Why are constants not constant - or not known...?
« Reply #3 on: May 23, 2020, 03:15:17 pm »
For use in case-statements you need to declare untyped constants:

Is this because a typed constant is actually a static variable (i.e. for OP's benefit can have its value changed and the changed value reappears the next time it's in scope)?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1523
  • Compiler Developer
Re: Why are constants not constant - or not known...?
« Reply #4 on: May 23, 2020, 04:10:42 pm »
For use in case-statements you need to declare untyped constants:

Is this because a typed constant is actually a static variable (i.e. for OP's benefit can have its value changed and the changed value reappears the next time it's in scope)?

More or less, yes.

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Why are constants not constant - or not known...?
« Reply #5 on: May 23, 2020, 07:04:23 pm »
For use in case-statements you need to declare untyped constants:
Is this because a typed constant is actually a static variable (i.e. for OP's benefit can have its value changed and the changed value reappears the next time it's in scope)?
More or less, yes.

So ... what if one sets {$J-} (or {$WRITEABLECONST OFF})? Can they then be used in a case statement or are they still "static" (frozen, rather ;)) variables?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8388
  • FPC developer.
Re: Why are constants not constant - or not known...?
« Reply #6 on: May 23, 2020, 07:06:40 pm »
Afaik they are still structured constants then (have a memory location, not pure constant), you just can't write them.

The syntax for a typed constant is with a cast like

Code: Pascal  [Select][+][-]
  1.   private const
  2.       a = byte(243);

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Why are constants not constant - or not known...?
« Reply #7 on: May 23, 2020, 08:51:20 pm »
The syntax for a typed constant is with a cast like
Code: Pascal  [Select][+][-]
  1.   private const
  2.       a = byte(243);

Ah ... sorry but, isn't that an untyped constant with, basically, a (strong) "hint" to the compiler to use that type for it? AFAIK, the sintax for a typed constant is:
Code: Pascal  [Select][+][-]
  1. const a: byte = 243;

Or are we losing ourselves in a terminology cloud? ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1523
  • Compiler Developer
Re: Why are constants not constant - or not known...?
« Reply #8 on: May 23, 2020, 08:59:24 pm »
For use in case-statements you need to declare untyped constants:
Is this because a typed constant is actually a static variable (i.e. for OP's benefit can have its value changed and the changed value reappears the next time it's in scope)?
More or less, yes.

So ... what if one sets {$J-} (or {$WRITEABLECONST OFF})? Can they then be used in a case statement or are they still "static" (frozen, rather ;)) variables?

They are still typed constants. Doesn't matter whether they're writable or not.

lucamar

  • Hero Member
  • *****
  • Posts: 2850
Re: Why are constants not constant - or not known...?
« Reply #9 on: May 23, 2020, 09:11:50 pm »
Afaik they are still structured constants then (have a memory location, not pure constant), you just can't write them.

They are still typed constants. Doesn't matter whether they're writable or not.

Yeah, I did the obvious and built a proggy to test it; it confirms both your answers.
Thanks to both of you.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 870
Re: Why are constants not constant - or not known...?
« Reply #10 on: May 23, 2020, 10:36:40 pm »
I admit to being disturbed by language designs which overload tokens like  const  and  extern  to mean different things depending on where they're used.

It's almost as though somebody interpreted the criticism of PL/I and Ada as "big languages" to mean that they had too many keywords, and resolved to avoid the trap by reusing existing tokens with their interpretation depending on context and order.

YOU ARE IN A MAZE OF TWISTY LITTLE PASSAGES, ALL DIFFERENT.
YOU ARE IN A LITTLE MAZE OF TWISTING PASSAGES, ALL DIFFERENT.
YOU ARE IN A MAZE OF TWISTING LITTLE PASSAGES, ALL DIFFERENT.
YOU ARE IN A LITTLE MAZE OF TWISTY PASSAGES, ALL DIFFERENT.
YOU ARE IN A TWISTING MAZE OF LITTLE PASSAGES, ALL DIFFERENT.
YOU ARE IN A TWISTING LITTLE MAZE OF PASSAGES, ALL DIFFERENT.
YOU ARE IN A TWISTY LITTLE MAZE OF PASSAGES, ALL DIFFERENT.
YOU ARE IN A TWISTY MAZE OF LITTLE PASSAGES, ALL DIFFERENT.
YOU ARE IN A LITTLE TWISTY MAZE OF PASSAGES, ALL DIFFERENT.
YOU ARE IN A MAZE OF LITTLE TWISTING PASSAGES, ALL DIFFERENT.
YOU ARE IN A MAZE OF LITTLE TWISTY PASSAGES, ALL DIFFERENT.


MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1523
  • Compiler Developer
Re: Why are constants not constant - or not known...?
« Reply #11 on: May 23, 2020, 10:44:14 pm »
I admit to being disturbed by language designs which overload tokens like  const  and  extern  to mean different things depending on where they're used.

Take that up with the developers of Turbo Pascal as that is where this comes from. FPC merely lives on that legacy.

MarkMLl

  • Hero Member
  • *****
  • Posts: 870
Re: Why are constants not constant - or not known...?
« Reply #12 on: May 23, 2020, 10:50:09 pm »
I admit to being disturbed by language designs which overload tokens like  const  and  extern  to mean different things depending on where they're used.

Take that up with the developers of Turbo Pascal as that is where this comes from. FPC merely lives on that legacy.

Also the developers of C++ and no doubt others. I wasn't specifically fingering (late) TP, Delphi or C++ for this, but I suspect that the era in which they were developed- broadly, the 1990s- had certain cultural sensitivities.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

440bx

  • Hero Member
  • *****
  • Posts: 1828
Re: Why are constants not constant - or not known...?
« Reply #13 on: May 24, 2020, 12:09:13 am »
but I suspect that the era in which they were developed- broadly, the 1990s- had certain cultural sensitivities.
I suspect the rather poor hack of typed constants stems from the fact that Borland didn't want to enhance the compiler to initialize local variables, by "overloading" (read: shameless bastardizing) the "const" keyword, they sidestepped that extra work.

Another solution, that would have accomplished the same thing, in a much cleaner and logical way, would have been to introduce a new keyword, such as "static".  Apparently, that was too much work too.

When it comes to "cultural sensibilities", it seems Borland has been more concerned about extracting money from customer's pockets than Pascal's integrity.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8388
  • FPC developer.
Re: Why are constants not constant - or not known...?
« Reply #14 on: May 24, 2020, 10:57:32 pm »
but I suspect that the era in which they were developed- broadly, the 1990s- had certain cultural sensitivities.
I suspect the rather poor hack of typed constants stems from the fact that Borland didn't want to enhance the compiler to initialize local variables, by "overloading" (read: shameless bastardizing) the "const" keyword, they sidestepped that extra work.

Or maybe they simply had the/a "const" segment in mind. Keep in mind that TP lived in a segmented world.

 

TinyPortal © 2005-2018