Jump to content
Ian Branch

Use of inline variables..

Recommended Posts

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 by Ian Branch

Share this post


Link to post
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 by Remy Lebeau

Share this post


Link to post

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
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
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
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? 

  • Like 1

Share this post


Link to post
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

The compiler says..

[dcc32 Error] Mainfrm.pas(424): E2029 Expression expected but 'ARRAY' found

Share this post


Link to post
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

🙂  I'm a simple man that does simple, old school, programming.  🙂  

Just trying to pick up some new tricks..

 

  • Like 1

Share this post


Link to post

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 by David Heffernan

Share this post


Link to post

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 by Ian Branch

Share this post


Link to post

...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;

 

  • Like 1

Share this post


Link to post
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

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 by Remy Lebeau
  • Like 1

Share this post


Link to post

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×