Recent

Author Topic: array initialization  (Read 2749 times)

fcu

  • Jr. Member
  • **
  • Posts: 89
array initialization
« on: September 23, 2018, 07:21:26 pm »
Hi
is it possible to do the same initailization with array of record ?
or this is juts for primitive types ?

Code: Pascal  [Select][+][-]
  1. function sum(ps : array of byte): longint;
  2. var i : longint;
  3. begin
  4.  result := 0;
  5.  for i:= 0 to high(ps) do
  6.   result += ps[i];
  7. end;
  8. begin
  9.  writeln(sum([1,2,3,4,5]));
  10.  readln;
  11. end.
  12.  
  13.  


furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: array initialization
« Reply #1 on: September 23, 2018, 07:30:06 pm »
You can use open array of record as parameter, for example:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Types;
  3.  
  4.   function Sum(const APoints: array of TPoint): Integer;
  5.   var
  6.     Point: TPoint;
  7.   begin
  8.     Result := 0;
  9.  
  10.     for Point in APoints do
  11.       Result += Point.X + Point.Y;
  12.   end;

but you need to have a function, that returns record, if you need to pass an array of values to function:

Code: Pascal  [Select][+][-]
  1. Write(Sum([Point(2, 2), Point(3, 3), Point(4, 4)]));

The Point above is a function from Types unit, that returns a TPoint structure, based on X and Y coords.
« Last Edit: September 23, 2018, 08:01:32 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: array initialization
« Reply #2 on: September 23, 2018, 09:46:50 pm »
In the upcoming FPC 3.2.0 you can also do this (assuming that Sum is declared as furious programming wrote):

Code: Pascal  [Select][+][-]
  1. const
  2.   MyPoints: array of TPoint = (
  3.     (x: 2; y: 2),
  4.     (x: 3; y: 3),
  5.     (x: 4; y: 4)
  6.   );
  7. begin
  8.   Writeln(Sum(MyPoints));
  9. end.    
  10.  

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: array initialization
« Reply #3 on: September 23, 2018, 10:07:28 pm »
You can use open array of record as parameter, for example:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Types;
  3.  
  4.   function Sum(const APoints: array of TPoint): Integer;
  5.   var
  6.     Point: TPoint;
  7.   begin
  8.     Result := 0;
  9.  
  10.     for Point in APoints do
  11.       Result += Point.X + Point.Y;
  12.   end;

but you need to have a function, that returns record, if you need to pass an array of values to function:

Code: Pascal  [Select][+][-]
  1. Write(Sum([Point(2, 2), Point(3, 3), Point(4, 4)]));

The Point above is a function from Types unit, that returns a TPoint structure, based on X and Y coords.

thanks , this is nice , at first i thought fpc may accept Tpoint instead of function point
Code: Pascal  [Select][+][-]
  1.  Write(Sum([TPoint(2, 2), TPoint(3, 3), TPoint(4, 4)]));
  2.  

it would be a killer feature  8-) , none of the langauges that i know allow this

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: array initialization
« Reply #4 on: September 23, 2018, 10:12:25 pm »
In the upcoming FPC 3.2.0 you can also do this (assuming that Sum is declared as furious programming wrote):

Code: Pascal  [Select][+][-]
  1. const
  2.   MyPoints: array of TPoint = (
  3.     (x: 2; y: 2),
  4.     (x: 3; y: 3),
  5.     (x: 4; y: 4)
  6.   );
  7. begin
  8.   Writeln(Sum(MyPoints));
  9. end.    
  10.  

yes it is already in trunk , it is really useful , except the writing of the name of fields which add more verbosity to the language

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: array initialization
« Reply #5 on: September 23, 2018, 10:30:33 pm »
You can use open array of record as parameter, for example:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Types;
  3.  
  4.   function Sum(const APoints: array of TPoint): Integer;
  5.   var
  6.     Point: TPoint;
  7.   begin
  8.     Result := 0;
  9.  
  10.     for Point in APoints do
  11.       Result += Point.X + Point.Y;
  12.   end;

but you need to have a function, that returns record, if you need to pass an array of values to function:

Code: Pascal  [Select][+][-]
  1. Write(Sum([Point(2, 2), Point(3, 3), Point(4, 4)]));

The Point above is a function from Types unit, that returns a TPoint structure, based on X and Y coords.

thanks , this is nice , at first i thought fpc may accept Tpoint instead of function point
Code: Pascal  [Select][+][-]
  1.  Write(Sum([TPoint(2, 2), TPoint(3, 3), TPoint(4, 4)]));
  2.  

it would be a killer feature  8-) , none of the langauges that i know allow this

ops i was wrong , c++11 list-initialization did just this

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: array initialization
« Reply #6 on: September 23, 2018, 10:44:47 pm »
thanks , this is nice , at first i thought fpc may accept Tpoint instead of function point

Code: Pascal  [Select][+][-]
  1. Write(Sum([TPoint(2, 2), TPoint(3, 3), TPoint(4, 4)]));

TPoint(2, 2) looks strange, like regular casting, but with many arguments.

If you want, you can use TPoint type in this way:

Code: Pascal  [Select][+][-]
  1. Write(Sum([TPoint.Create(2, 2), TPoint.Create(3, 3), TPoint.Create(4, 4)]));

Yes, more code to write, but it is correct too.

it would be a killer feature  8-) , none of the langauges that i know allow this

This construction is too much similar to regular casting, so rather it will never be used in Free Pascal.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018