Nice feature of sets lazarus / 61-Beginners ---------------------------------------------------------------------- simsee · 2020-05-02 19:36 I have always used sets where the type of elements is defined in the type section. I had never noticed that it's possible to build sets on the fly that contain variable elements, for example to be used in a for..in loop Eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   var  ind,k,l : integer; begin  k:=3;  l:=50;  for ind in [1..k,l] do    writeln(ind); end. Interesting feature, not explicitly documented, or at least not obvious to me. For this reason I thought of sharing it in the beginners section. ---------------------------------------------------------------------- Leledumbo · 2020-05-02 19:56 Quote from: simsee on May 02, 2020, 09:36:19 pmInteresting feature, not explicitly documented, or at least not obvious to me. For this reason I thought of sharing it in the beginners section. https://www.freepascal.org/docs-html/ref/refse79.html, part of https://www.freepascal.org/docs-html/ref/refch12.html. ---------------------------------------------------------------------- ASerge · 2020-05-02 21:58 Quote from: Leledumbo on May 02, 2020, 09:56:59 pmhttps://www.freepascal.org/docs-html/ref/refse79.html, part of https://www.freepascal.org/docs-html/ref/refch12.html. I think this is more appropriate The For..in..do statement 2. A set value. The loop will then be over all elements in the set, the control variable must be of the base type of the set. ---------------------------------------------------------------------- simsee · 2020-05-02 22:18 Thanks. I know these documents. This is the key point: "A set constructor is a comma separated list of expressions, enclosed in square brackets." In my mind was the wrong idea that elements of a set must be constant. An expression can have variables. ---------------------------------------------------------------------- 440bx · 2020-05-02 23:32 I just wanted to mention that when using variables to specify the bounds of a set, the results may not be as expected as the sample code shows:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE} program TestSets; var  a, b : cardinal;   c    : cardinal; begin  { case 1}   a := 1;   b := high(cardinal);   c := 23000;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes') else writeln('no - wrong');  writeln;   b := high(cardinal) div 8;   c := b * 2; { case 2 }  { correct but, just happenstance }   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes') else writeln('no - pure happenstance');  writeln; { case 3 }   b := 25;  c := 12;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes - correct') else writeln('no');  writeln; { case 4 }   a := 200;  b := 400;  c := 301;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes - correct') else writeln('no - wrong');  writeln;end.  ETA: and there won't be any compiler warnings about the range not being in the bounds of what the compiler expects. ---------------------------------------------------------------------- simsee · 2020-05-03 08:00 Are these incorrect behaviors documented? The sets used in this way do not seem reliable ... ---------------------------------------------------------------------- howardpc · 2020-05-03 08:39 I was not aware that FPC allowed you to construct a set of variables in code in this way. However, whoever implemented it clearly left a bug, since the compiler should at least check that the variables in the set are Byte or Char (or some subrange, or an enumeration within Byte range), and balk if this condition is not met. ---------------------------------------------------------------------- munair · 2020-05-03 09:00 Quote from: 440bx on May 03, 2020, 01:32:50 am..to specify the bounds of a set.. Strictly speaking, in your example there is no set. My question in this thread regarding the 'in' operator is related to your example. I've seen code before where the operator was plainly used like that without overload or set definition. Not good. ---------------------------------------------------------------------- simsee · 2020-05-03 09:05 Quote from: howardpc on May 03, 2020, 10:39:02 amI was not aware that FPC allowed you to construct a set of variables in code in this way. However, whoever implemented it clearly left a bug, since the compiler should at least check that the variables in the set are Byte or Char (or some subrange, or an enumeration within Byte range), and balk if this condition is not met. This confirms my initial thesis: this is an aspect not explicitly documented. In light of the 440bx example, I add that it is a seriuos bug in a core feature of the language. ---------------------------------------------------------------------- simone · 2020-05-03 09:23 Consider the output of the following example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;var  N,I : integer; begin  N:=256;  for I in [0..N] do    writeln(I);  readln;end. The program shows only one line with 0. This leads me to think that when an element of a set has more than 8 bits (i.e. is not a char, byte etc), the least significant byte is taken into account. ---------------------------------------------------------------------- munair · 2020-05-03 09:36 Quote from: simone on May 03, 2020, 11:23:04 amThe program shows only one line with 0. Did you expect 257 iterations? QuoteThis leads me to think that when an element of a set has more than 8 bits (i.e. is not a char, byte etc), the least significant byte is taken into account. Obviously only the first 8 bits are considered. But FPC should check the word size definitely. ---------------------------------------------------------------------- eljo · 2020-05-03 09:38 Quote from: munair on May 03, 2020, 11:36:29 amQuote from: simone on May 03, 2020, 11:23:04 amThis leads me to think that when an element of a set has more than 8 bits (i.e. is not a char, byte etc), the least significant byte is taken into account. Obviously only the first 8 bits are considered. But FPC should check the word size definitely. No the first 255 values are considered. A set with 255 values is 255 bits long. But yeah an out of bounds or variable to big or something should be raised I guess. ---------------------------------------------------------------------- munair · 2020-05-03 09:52 Quote from: eljo on May 03, 2020, 11:38:49 amQuote from: munair on May 03, 2020, 11:36:29 amQuote from: simone on May 03, 2020, 11:23:04 amThis leads me to think that when an element of a set has more than 8 bits (i.e. is not a char, byte etc), the least significant byte is taken into account. Obviously only the first 8 bits are considered. But FPC should check the word size definitely. No the first 255 values are considered. A set with 255 values is 255 bits long. But yeah an out of bounds or variable to big or something should be raised I guess. Yeah, that was explained before, but it's confusing. ---------------------------------------------------------------------- PascalDragon · 2020-05-03 13:23 Seems like no one checked with enabled overflow and range checks... The example by 440bx extended by exception handling code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE} program tsettest; {$mode objfpc} {$OverflowChecks On}{$RangeChecks On} uses  SysUtils; var  a, b : cardinal;   c    : cardinal; begin  { case 1}   a := 1;   b := high(cardinal);   c := 23000;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  try    if c in [a..b] then writeln('yes') else writeln('no - wrong');  except    on e: Exception do      Writeln('Exception (', e.ClassName, '): ', e.Message);  end;  writeln;   b := high(cardinal) div 8;   c := b * 2; { case 2 }  { correct but, just happenstance }   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  try    if c in [a..b] then writeln('yes') else writeln('no - pure happenstance');  except    on e: Exception do      Writeln('Exception (', e.ClassName, '): ', e.Message);  end;  writeln; { case 3 }   b := 25;  c := 12;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  try    if c in [a..b] then writeln('yes - correct') else writeln('no');  except    on e: Exception do      Writeln('Exception (', e.ClassName, '): ', e.Message);  end;  writeln; { case 4 }   a := 200;  b := 400;  c := 301;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  try    if c in [a..b] then writeln('yes - correct') else writeln('no - wrong');  except    on e: Exception do      Writeln('Exception (', e.ClassName, '): ', e.Message);  end;  writeln;end. Output: Code: [Select]a = 1 b = 4294967295 c = 23000 c in [a..b] ? : Exception (ERangeError): Range check error a = 1 b = 536870911 c = 1073741822 c in [a..b] ? : Exception (ERangeError): Range check error a = 1 b = 25 c = 12 c in [a..b] ? : yes - correct a = 200 b = 400 c = 301 c in [a..b] ? : Exception (ERangeError): Range check error Quote from: simsee on May 03, 2020, 11:05:22 amQuote from: howardpc on May 03, 2020, 10:39:02 amI was not aware that FPC allowed you to construct a set of variables in code in this way. However, whoever implemented it clearly left a bug, since the compiler should at least check that the variables in the set are Byte or Char (or some subrange, or an enumeration within Byte range), and balk if this condition is not met. This confirms my initial thesis: this is an aspect not explicitly documented. In light of the 440bx example, I add that it is a seriuos bug in a core feature of the language. The limits of sets are documented here: QuoteEach of the elements of SetType must be of type TargetType. TargetType can be any ordinal type with a range between 0 and 255. A set can contain at most 255 elements. The compiler checks for these bounds either at compile time (if you're dealing with constants) or at runtime (if the values aren't constant and range checks are enabled). ---------------------------------------------------------------------- 440bx · 2020-05-03 15:36 Quote from: PascalDragon on May 03, 2020, 03:23:34 pmThe compiler checks for these bounds either at compile time (if you're dealing with constants) or at runtime (if the values aren't constant and range checks are enabled). Even when dealing with constants, it doesn't always check at compile time, e.g, it allows a constant array subscript out of the array's declared bounds, which is incorrect.  Delphi doesn't... it does it right.   You and Jonas have already declined to correct that error, therefore, I am not asking for it to be corrected, just pointing out that FPC does less than it could do (and should do) at compile time. I'll give you another opportunity to say "no" again to a different thing here it is... in case 4, which is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { case 4 }   a := 200;  b := 400;  c := 301;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes - correct') else writeln('no - wrong');  writeln;end. The number of elements in the set bound by "a" to "b" (200..400) is less than 256, therefore, that is a case where the compiler could use its set algorithms to determine if "c" is in the set but, for that, it would need to "rebase" the set (on the fly when generating code).  That's one of the unfortunate limitations of sets in both FPC and Delphi which is, sets are not only limited to 256 elements but, the first element's ordinal _must_ be zero. That last limitation doesn't really need to be there.  It could be removed (hint, hint) NOTE: even though I am reusing the example where the bounds are variables, FPC would refuse to compile that code even if "a" and "c" where constants because the base of the set (its first element is not zero (0)). ---------------------------------------------------------------------- PascalDragon · 2020-05-03 18:02 Quote from: 440bx on May 03, 2020, 05:36:54 pmQuote from: PascalDragon on May 03, 2020, 03:23:34 pmThe compiler checks for these bounds either at compile time (if you're dealing with constants) or at runtime (if the values aren't constant and range checks are enabled). Even when dealing with constants, it doesn't always check at compile time, e.g, it allows a constant array subscript out of the array's declared bounds, which is incorrect.  Delphi doesn't... it does it right.   You and Jonas have already declined to correct that error, therefore, I am not asking for it to be corrected, just pointing out that FPC does less than it could do (and should do) at compile time. FPC does warn however and a user can easily elevate this to an error. This way users can at least use one element arrays for tricks with APIs (like the Windows API which has some structs with Bla[] at the end). Quote from: 440bx on May 03, 2020, 05:36:54 pmI'll give you another opportunity to say "no" again to a different thing here it is... in case 4, which is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { case 4 }   a := 200;  b := 400;  c := 301;   writeln('a = ', a, ' b = ', b, ' c = ', c);  write  ('c in [a..b] ? : ');  if c in [a..b] then writeln('yes - correct') else writeln('no - wrong');  writeln;end. The number of elements in the set bound by "a" to "b" (200..400) is less than 256, therefore, that is a case where the compiler could use its set algorithms to determine if "c" is in the set but, for that, it would need to "rebase" the set (on the fly when generating code).  That's one of the unfortunate limitations of sets in both FPC and Delphi which is, sets are not only limited to 256 elements but, the first element's ordinal _must_ be zero. That last limitation doesn't really need to be there.  It could be removed (hint, hint) Technically this would of course be possible, but this would simply lead to more confusion in the end. Currently a set is defined as something that can have elements with values from 0 to 255. Allowing the rebasing of sets would contradict this. ---------------------------------------------------------------------- 440bx · 2020-05-03 19:13 Quote from: PascalDragon on May 03, 2020, 08:02:21 pmCurrently a set is defined as something that can have elements with values from 0 to 255. Allowing the rebasing of sets would contradict this. Yes but, as Howardpc mentioned, it's quite obvious that a cardinal type has quite a few more than 256 elements and the compiler _knows_ that and it also knows that the set range being used is of that type, yet it allows it.  If you're going to apply the rule that a set can only have 256 elements then, it's obvious the variables that determine the boundaries of the set cannot be of a type that go well beyond that range. If the compiler is going to allow variables of a greater range then it needs to rebase and simply enforce the 256 element rule. Otherwise, as you stated, the behavior is confusing. ---------------------------------------------------------------------- PascalDragon · 2020-05-04 07:04 Quote from: 440bx on May 03, 2020, 09:13:04 pmQuote from: PascalDragon on May 03, 2020, 08:02:21 pmCurrently a set is defined as something that can have elements with values from 0 to 255. Allowing the rebasing of sets would contradict this. Yes but, as Howardpc mentioned, it's quite obvious that a cardinal type has quite a few more than 256 elements and the compiler _knows_ that and it also knows that the set range being used is of that type, yet it allows it.  If you're going to apply the rule that a set can only have 256 elements then, it's obvious the variables that determine the boundaries of the set cannot be of a type that go well beyond that range. If the compiler is going to allow variables of a greater range then it needs to rebase and simply enforce the 256 element rule. Otherwise, as you stated, the behavior is confusing. The compiler also allows you to assign a LongWord to a Byte without any typecast and will only complain at runtime if you have range checks enabled and the value does not fit the range of Byte. Thus the current behaviour with sets is in line with that behaviour.