Forum > General

strange behaviour with nested WITH statements.

(1/4) > >>

Bitbeisser:
In another programming forum, there was recently a discussion about scope and possible ambiguity of references to equally named (and typed) elements.
I created the following test program and was surprised that the following even compiled without an error (actually, kind of proving the ambiguity, though I would have expected that the assignment to BufferB [Counter] would actually throw at least one compiler error

--- 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";}};} ---Program Withscopetest; Var A : Record          Counter : Integer;          BufferA : Array [1..100] of Byte;        end; // A    B : Record          Counter : Integer;          BufferB : Array [101..200] of Char;        end; // Bbegin  A.Counter := -1;  B.Counter := -1;  FillChar (A.BufferA, 100, 0);  FillChar (B.BufferB, 100, '?');  with A do    with B do      begin        Counter := 1;        BufferA [Counter] := Counter;        BufferB [Counter] := Char (Counter+48);      end;  WriteLn (A.BufferA [1]);  WriteLn (B.BufferB [101]);  ReadLn;end. 
What am I missing here?  :-\

PS: Sorry, but what I missed in my original post, this is with FPC 3.2.2 (the one that comes with Lazarus 3.2), 64 bit on Windows 10/64 bit

Josh:
When I set the for debugmode then I Get Range Error as expected

440bx:
That behavior has nothing to do with the usage of "with" (no pun intended.)  FPC is a bit "peculiar" when it comes to enforcing array bounds.

Not only are array bounds not really enforced by the compiler but, they are managed in a very "unique" way.  Have a look at the sample program in the message:
https://forum.lazarus.freepascal.org/index.php/topic,44655.msg314233.html#msg314233
and make it a point to read the comments that mention the differences between FPC and Delphi in the treatment of array indexes.

Thaddy:
Apart from the scoping ambiguity indeed, when compiled with {$rangechecks on} you will get an error.
That this compiles without error if range checks are off is correct, because code can be written on purpose to make use of overflow of a range. E.g. the index of an array[0..255]  becomes zero after index 255 is reached in $R- state, but will throw an error in $R+ state.( code is a simple ringbuffer and legal. This works in both FPC and D7.)
--- 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";}};} ---program ringbuffers;{$R-}{$ifdef fpc}{$mode delphi}{$endif}{$apptype console}var  RB: array[0..255] of double;   b:byte;begin  b:=0;  repeat    writeln(b);    inc(b);  until true = false;  // ctr-c to quitend.A practical application that I use myself to write continuously running oscillators for sound processing.
The array contains the wave shape.

Warfley:
Yeah with introduces scoping issues with shadowing, not just in this example but general (e.g. with over a control in an lack application hides the form properties like caption, color, etc.)

With is actually a terrible construct, so bad that even JavaScript, a language notorious for always keeping backwards compatibility even for the most awful features, deprecated it.
If even JavaScript devs say that this is to bad to even keep for backwards compatibility, it's a really bad feature

Navigation

[0] Message Index

[#] Next page

Go to full version