Ian Branch 127 Posted November 8, 2022 (edited) Hi Team, OK, so I'm a bit slow coming to the party. I am just starting to have a serious look at inline variables. I have read a couple o articles on the subject and can deal with most uses however there are a couple that I am unsure of the consequences.. If I have code something like this.. ... ... for var i: SmallInt := 0 to Screen.DataModuleCount - 1 do begin var TheDMod:TDatamodule := TDatamodule(Screen.DataModules[i]); ... .. end; Will 'TheDMod' get redeclared every iteration of i? Are there any dangers in doing this?? Similarly for.. ... for var i: SmallInt := 0 to Screen.FormCount - 1 do begin var TheForm: TForm := Screen.Forms[i]; ... ... end; I discovered that this isn't acceptable.. function ComputerName: string; begin var Size: DWord := 256; var buffer: array [0 .. 255] of Char; Result := IfThen(GetComputerName(buffer, Size), buffer, ''); end; It doesn't like the var buffer.... line. I also discovered it throws my code formatting out. 😉 Regards & TIA, Ian Edited November 8, 2022 by Ian Branch Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 The biggest issue is the use of Smallint Share this post Link to post
Remy Lebeau 1396 Posted November 8, 2022 (edited) 26 minutes ago, Ian Branch said: If I have code something like this.. ... Will 'TheDMod' get redeclared every iteration of i? Yes. Its scope is limited to the loop body, so it will enter and leave scope on each iteration. Quote Are there any dangers in doing this?? Not really, no. If ARC were involved (which is no longer the case), that would include incrementing and decrementing the object's refcount. But in any case, since the variable is just a pointer, the compiler is likely to optimize the variable to reuse the same memory for each new instance each time the loop iterates. Quote I discovered that this isn't acceptable.. ... It doesn't like the var buffer.... line. What does the error message actually say? It looks OK ome, so I'm guessing that the compiler simply hasn't implemented support for inline arrays. Do you have the same problem if you declare the array type beforehand? type Char256Array = array [0 .. 255] of Char; var buffer: Char256Array; Quote I also discovered it throws my code formatting out. 😉 Last I heard, the CodeEditor/ErrorInsight still hadn't been updated to support the new inline syntax yet. But the compiler will still accept it. Edited November 8, 2022 by Remy Lebeau Share this post Link to post
Ian Branch 127 Posted November 8, 2022 Hi Team, Thank you for your responses. David - What is the issue with smallint?? Remy - Thank you for the clarifications.. The "array" and "of" have the squiggly red line under them. For array - 'Expression expected but 'ARRAY' found'. And, for of - 'Type identifier expected'. Regards, Ian Share this post Link to post
Ian Branch 127 Posted November 8, 2022 P.S. I'm loving the Type inference... Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 9 minutes ago, Ian Branch said: What is the issue with smallint?? You should use Integer for loop variables otherwise some day you'll be caught out with an unexpected overflow. Share this post Link to post
Ian Branch 127 Posted November 8, 2022 Just now, David Heffernan said: You should use Integer for loop variables otherwise some day you'll be caught out with an unexpected overflow. I though that might have been your reservation. I got rid of the smallint and left it to type inference. 🙂 Cheers. Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 11 minutes ago, Ian Branch said: The "array" and "of" have the squiggly red line under them. For array - 'Expression expected but 'ARRAY' found'. And, for of - 'Type identifier expected' Never mind what the ide says. What does the compiler say? 1 Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 Just now, Ian Branch said: I though that might have been your reservation. I got rid of the smallint and left it to type inference. 🙂 Cheers. In reality you aren't going to have 32k forms. However as a general rule you don't want to be thinking about stuff like that. Just use Integer everywhere and avoid it being something to consider. Now, there are sometimes situations where a 64 bit loop variable is required but they are exceptional. Share this post Link to post
Ian Branch 127 Posted November 8, 2022 The compiler says.. [dcc32 Error] Mainfrm.pas(424): E2029 Expression expected but 'ARRAY' found Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 6 minutes ago, Ian Branch said: P.S. I'm loving the Type inference... Wait until you have a bit more complexity and then you won't love it so much! Share this post Link to post
Ian Branch 127 Posted November 8, 2022 🙂 I'm a simple man that does simple, old school, programming. 🙂 Just trying to pick up some new tricks.. 1 Share this post Link to post
David Heffernan 2345 Posted November 8, 2022 (edited) Docs for GetComputerName say "A pointer to a buffer that receives the computer name or the cluster virtual server name. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters." You should do that. Use the constant in your code. Edited November 8, 2022 by David Heffernan Share this post Link to post
Ian Branch 127 Posted November 8, 2022 (edited) Noted. function ComputerName: string; var buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char; begin var Size: Cardinal := MAX_COMPUTERNAME_LENGTH + 1; GetComputerName(@buffer, Size); Result := StrPas(buffer); end; Edited November 8, 2022 by Ian Branch Share this post Link to post
Anders Melander 1783 Posted November 8, 2022 ...or without the temporary buffer: function ComputerName: string; begin var Size: Cardinal := 0; if (not GetComputerName(PChar(Result), Size)) and (GetLastError <> ERROR_BUFFER_OVERFLOW) then RaiseLastOSError; SetLength(Result, Size-1); if (not GetComputerName(PChar(Result), Size)) then RaiseLastOSError; end; 1 Share this post Link to post
Remy Lebeau 1396 Posted November 8, 2022 1 hour ago, Ian Branch said: What is the issue with smallint?? List counts are 32bit Integers, not 16bit Smallints. I doubt you will ever have more than 32K Forms/DataModules active at a time, but it is less efficient to make the compiler compare a Smallint loop counter to an Integer count on every iteration, the counter would have to be scaled up to 32bits each time. 1 hour ago, Ian Branch said: The "array" and "of" have the squiggly red line under them. For array - 'Expression expected but 'ARRAY' found'. And, for of - 'Type identifier expected'. Is that an actual compiler error? Or just an ErrorInsight error, but the code actually compiles fine? Share this post Link to post
Remy Lebeau 1396 Posted November 8, 2022 (edited) Apparently, this is a known bug since inline variables were first introduced in 10.3 Rio, and the following tickets are still open: RSP-22359: Nameless types are not allowed within inline variable declarations RSP-31970: How to inline static array variables in Rio Based on the comments on the tickets, the workaround I suggested earlier to pre-define the array type should work for an inline variable: type Char256Array = array [0 .. 255] of Char; var buffer: Char256Array; Edited November 8, 2022 by Remy Lebeau 1 Share this post Link to post
Ian Branch 127 Posted November 8, 2022 Tks Remy. I have Voted for them. Ian Share this post Link to post
Fr0sT.Brutal 900 Posted November 9, 2022 Did they make inline record constants available yet? 10.3 that I have at hand can't do this. Share this post Link to post
omnibrain 15 Posted November 9, 2022 I ran into a problem, that the debugger (in 11.2) does not seem to understand if you use the same name in tow places within one function. case geocoder of gc_pos: begin //... var lat:=posdata.data[0].latitude; var long:=posdata.data[0].longitude; //... end; gc_nom: begin //.. var lat:=position.lat; var long:=position.lon; //.. end; //... end; When debugging into the gc_nom case the debugger showed me uninitialised values ever after the assignement. Share this post Link to post
Stefan Glienke 2002 Posted November 9, 2022 (edited) https://quality.embarcadero.com/browse/RSP-23096 Edited November 9, 2022 by Stefan Glienke 1 1 Share this post Link to post