Strange result at with result do lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2024-06-30 17:45 Hi, See TestFun_1. I understand that it is not useful to call all variables "S", but why is the incoming s not assigned to the result s? TestFun_2 works well. The only difference is that 1 var is now renamed to t. I tested in lazarus 3.4, 3.0 and 2.4. (on Windows). All 3 give the same result. 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  PDummy = ^TDummy;  TDummy = record    s: String;  end;    { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    function TestFun_1(s: String): TDummy;    function TestFun_2(t: String): TDummy;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  lRec : TDummy;begin  lRec := TestFun_1('some text');  Edit1.Text:= lRec.s;end; procedure TForm1.Button2Click(Sender: TObject);var  lRec : TDummy;begin  lRec := TestFun_2('some text');  Edit1.Text:= lRec.s;end;  function TForm1.TestFun_1(s: String): TDummy;begin  With Result do begin    s:= s;   // Returns nothing even the incoming "s: String" holds a value.  end;end; function TForm1.TestFun_2(t: String): TDummy;begin  With Result do begin    s:= t;  // works fine.  end;end; end. ---------------------------------------------------------------------- jamie · 2024-06-30 18:00 that's because both "S" are the same one. The result S is empty and you just setting itself to itself. the WITH is localizing the scope to the single S ---------------------------------------------------------------------- WooBean · 2024-06-30 18:06 Quote from: Hansvb on June 30, 2024, 07:45:25 pmHi, See TestFun_1. I understand that it is not useful to call all variables "S", but why is the incoming s not assigned to the result s? TestFun_2 works well. The only difference is that 1 var is now renamed to t. I tested in lazarus 3.4, 3.0 and 2.4. (on Windows). All 3 give the same result. 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  function TForm1.TestFun_1(s: String): TDummy;begin  With Result do begin    s:= s;   // Returns nothing even the incoming "s: String" holds a value.  end;end;   Nothing strange - where is "s" declaration searched? First - inside Result record, as "s" field is found then is used. In Pascal 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   s:=s;   does nothing. 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  function TForm1.TestFun_1(s: String): TDummy;begin  Result.s:=s; //it will do what you wantend; ---------------------------------------------------------------------- cdbc · 2024-06-30 18:14 Hi IMHO this: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   PDirectoriesRec = ^TDirectoriesRec;  TDirectoriesRec = record    dirNewDirnames, { TODO }    dirProjectName, { dunno, could be useful. ==> Now it is :) }    dirRoot,    dirSuccesMsg: string;    dirPrjNameInDir: Boolean;    dirSucces: boolean;  end; is a much better naming-convention i.e.: prefixing the fields in the record with the versals of the record type-name... in the above case 'dir'. That way if you can't remember the fields' names then you can, at least figure it out and avoid name-clashes... Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-06-30 18:18 Close, but not completely correct: what happens is that the result member variable s from TDummy as different scope. so you get with Tdummy do ({TDummy.}s} := {Tdummy.}s But the compiler warned you implicitly for that: warning result variable seems not initialized and hint parameter s is not used. Then again, ignoring warnings - and hints - , naming both s and using with all in the same line of code isn't very sane. ---------------------------------------------------------------------- WooBean · 2024-06-30 18:29 Quote from: Thaddy on June 30, 2024, 08:18:37 pmClose, but not completely correct: what happens is that the result variable s from TDummy as different scope than the higher scoped parameter s. But the compiler warned you implicitly for that: warning result variable seems not initialized. Then again, ignoring warnings, naming both s and using with all in the same line of code isn't very sane. To make an exercise more interesting I'd suggest a change  in type TForm1: 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 TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    function TestFun_1(s: String): TDummy;    function TestFun_2(t: String): TDummy;  public    s:string;//then try to distinguish parameter of  function TestFun_1(s: String): TDummy;  from class field.    end; ---------------------------------------------------------------------- Thaddy · 2024-06-30 18:35 To me it is an interesting excercise if you pay attention to warnings and hints:  Hint: (5024) Parameter "s" not used  Warning: (5059) Function result variable does not seem to be initialized ---------------------------------------------------------------------- Hansvb · 2024-06-30 19:26 The s parameter was for convenience in the example. But I did not look at the hint or warnings. I'm going to check it out. ---------------------------------------------------------------------- Martin_fr · 2024-06-30 19:34 It seems you expected something like JS: Code: Javascript  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var x = x;where the 2nd x is from the scope before the var. But "with" isn't declaring "result". So any variable in the block (or statement) that is covered by the "with" is treated equally. 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 with result do s := s;translates to 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 result.s := result.s; ---------------------------------------------------------------------- Thaddy · 2024-06-30 19:37 If you click on the hint the IDE shows you that the parameter is not used. If you click on the warning it jumps to s:=s which is result.s := result.s and not initialized. The combination of both tells you the block scope is with and the compiler has no other information to solve that.