Recent

Author Topic: Variant Record Using Cases With Ranges  (Read 538 times)

simone

  • Hero Member
  • *****
  • Posts: 626
Variant Record Using Cases With Ranges
« on: October 31, 2024, 11:09:25 am »
Today for the first time I had the crazy idea to write a variant record like the following:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode Delphi}
  3.  
  4. type
  5.   TShape=(shRectangle, shPentagon, shExagon, shEllipse);
  6.  
  7.   TBorderPoint=record
  8.     case TShape of
  9.       shEllipse             : (Angle : real);
  10.       shRectangle..shExagon : (Segment, Position : integer);
  11.     end;
  12.  
  13.   procedure Proc;
  14.   begin
  15.   end;
  16.  
  17. begin
  18. end.

The compiler, both in ObjFpc and Delphi modes, did not issue errors, but as you can see in the attached screenshot, the codetools, at some point, complains.

Given that what is shown in the example can easily be achieved in another way, my question is whether in a variant record it is allowed to use a case having ranges (and if so, there is a bug in codetools) or it is not allowed.

From the examination of the syntactic diagrams in

https://www.freepascal.org/docs-html/current/ref/refsu15.html

it would seem possible to have enumerations, but not ranges.

Thanks.

« Last Edit: October 31, 2024, 02:43:11 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: Variant Record Using Cases With Ranges
« Reply #1 on: October 31, 2024, 11:20:30 am »
https://wiki.freepascal.org/Record#Variable_structure

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode Delphi}
  3.  
  4. type
  5.   TShape=(shRectangle, shPentagon, shExagon, shEllipse);
  6.  
  7.   TBorderPoint=record
  8.     case TShape of
  9.       shEllipse             : (Angle : real);
  10.       shRectangle,shPentagon,shExagon : (Segment, Position : integer);
  11.     end;
  12.  
  13.   procedure Proc;
  14.   begin
  15.   end;
  16.  
  17. begin
  18. end.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

simone

  • Hero Member
  • *****
  • Posts: 626
Re: Variant Record Using Cases With Ranges
« Reply #2 on: October 31, 2024, 11:22:48 am »
Dear Zvoni, as I wrote, I know how to write an equivalent record. My question is another.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

440bx

  • Hero Member
  • *****
  • Posts: 4731
Re: Variant Record Using Cases With Ranges
« Reply #3 on: October 31, 2024, 11:36:59 am »
@simone,

Interesting case you found.

Delphi v2 does _not_ accept the "shRectangle..shExagon" construct in the variant.

I have to admit that I have not checked what the last Pascal standard says about the construction of variants but, I believe the code you presented should work because 1. variants are identified using "case" and ranges are valid in a "case"  2. the ".." is just a way to abbreviate multiple possibilities, it has no semantic effect therefore it should be valid (IOW, there is no good reason to make it invalid.)

Summary: I believe FPC is correct in accepting that construction and, codetools should accept it too.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 4731
Re: Variant Record Using Cases With Ranges
« Reply #4 on: October 31, 2024, 11:46:55 am »
I just checked the ISO 1990 standard Pascal (not the extended 1991 Pascal standard) and, that standard does _not_ allow specifying a range in the selector.

I don't have a copy of the extended Pascal standard (at least not one I could find on my hard drive)

ETA:

Found the 1990 extended Pascal standard and that one does allow specifying a range.

It also allows specifying "otherwise".

HTH.
« Last Edit: October 31, 2024, 11:51:16 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

simone

  • Hero Member
  • *****
  • Posts: 626
Re: Variant Record Using Cases With Ranges
« Reply #5 on: October 31, 2024, 12:04:12 pm »
@simone,

Interesting case you found.

Delphi v2 does _not_ accept the "shRectangle..shExagon" construct in the variant.

I have to admit that I have not checked what the last Pascal standard says about the construction of variants but, I believe the code you presented should work because 1. variants are identified using "case" and ranges are valid in a "case"  2. the ".." is just a way to abbreviate multiple possibilities, it has no semantic effect therefore it should be valid (IOW, there is no good reason to make it invalid.)

Summary: I believe FPC is correct in accepting that construction and, codetools should accept it too.


Totally agree. The question is irrelevant from a semantic point of view. It's only a curiosity about syntax. Thanks.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Warfley

  • Hero Member
  • *****
  • Posts: 1750
Re: Variant Record Using Cases With Ranges
« Reply #6 on: October 31, 2024, 01:21:30 pm »
According to the documentation, it is not intended: https://www.freepascal.org/docs-html/ref/refsu15.html (see the syntax diagram)
But the parser indeed allows it. The interesting part of this is that looking at the source of the compiler:
Code: Pascal  [Select][+][-]
  1. { iso pascal does not support ranges in variant record definitions }
  2.                   if (([m_iso,m_extpas]*current_settings.modeswitches)=[]) and try_to_consume(_POINTPOINT) then
  3.                     pt:=crangenode.create(pt,comp_expr([ef_accept_equal]))
  4.                   else
  5.                     begin
  6.                       with variantdesc^^.branches[high(variantdesc^^.branches)] do
  7.                         begin
  8.                           SetLength(values,length(values)+1);
  9.                           values[high(values)]:=tordconstnode(pt).value;
  10.                         end;
  11.                     end;
It is not just supported "by accident" but it is fully supported on purpose, and it specifically mentions that it is only not supported in ISO mode.

So I would say that the documentation and lazarus is "wrong" in that they do not match the intend of the fpc developers when building this

simone

  • Hero Member
  • *****
  • Posts: 626
Re: Variant Record Using Cases With Ranges
« Reply #7 on: October 31, 2024, 02:48:08 pm »
According to the documentation, it is not intended: https://www.freepascal.org/docs-html/ref/refsu15.html (see the syntax diagram)

I had reported this issue of documentation in my first post

It is not just supported "by accident" but it is fully supported on purpose, and it specifically mentions that it is only not supported in ISO mode.

So I would say that the documentation and lazarus is "wrong" in that they do not match the intend of the fpc developers when building this

Thanks for this valuable clarification.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018