Re: [fpc-devel] Array assignment operator

2018-11-05 Thread Sven Barth via fpc-devel
Am Mo., 5. Nov. 2018, 01:00 hat Gennady Agranov 
geschrieben:

> On 11/4/2018 5:24 PM, Sven Barth via fpc-devel wrote:
>
> Am So., 4. Nov. 2018, 20:36 hat Gennady Agranov 
> geschrieben:
>
>> On 11/4/2018 11:24 AM, Sven Barth via fpc-devel wrote:
>>
>> Am So., 4. Nov. 2018, 12:23 hat Schindler Karl-Michael <
>> karl-michael.schind...@web.de> geschrieben:
>>
>>>
>>>
>>> > Am 04.11.2018 um 12:00 schrieb Ben Grasset :
>>> >
>>> > From: Ben Grasset 
>>> > To: FPC-Devel users discussions 
>>> > Subject: Re: Array assignment operator
>>> > Message-ID: <
>>> cal4d7fjpfx-yst6z3--fhpr9pts-n47ksfn6m2phd7sunzw...@mail.gmail.com>
>>> > Content-Type: text/plain; charset="utf-8"
>>> >
>>> > On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov <
>>> gennadyagra...@gmail.com>
>>> > wrote:
>>> >
>>> >> Hi,
>>> >>
>>> >> Leaving aside the reason why the MiSchi's solution doesn't work the
>>> main
>>> >> question is still not answered :)
>>> >>
>>> >> If you have integer dynamic array "MyArray" is there a way for the
>>> >> following statement to compile and work correctly:
>>> >>
>>> >> MyArray := 5;
>>> >>
>>> >
>>> > Uh, yes? That's what my example showed.
>>>
>>> In your example the length of the array was set to the number and the
>>> elements of the array assigned to their index, whereas my intention is to
>>> keep the length of the array and fill all elements to the same number.
>>>
>>
>> The operator always creates a new array and does not modify the existing
>> one.
>> You'd need to abuse a binary operator (e.g. >< or even <=) for this.
>>
>> Regards,
>> Sven
>>
>>>
>>
>> ___
>> fpc-devel maillist  -  
>> fpc-devel@lists.freepascal.orghttp://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>>
>> I was thinking that type helper might work - e.g. MyArray.SetValue(5) and
>> with using properties MyArray.Value := 5
>>
>> But seems that even simple type helper is not working anymore?
>>
>> E.g. I was not able to compile example from
>> http://wiki.freepascal.org/Helper_types :(
>>
>> TTypeHelper = type helper for Integer
>>   procedure SetZero;end;
>>
>> Did you add {$modeswitch typehelpers}?
>
> If so, what is the error you get?
>
> Regards,
> Sven
>
>>
>
> ___
> fpc-devel maillist  -  
> fpc-devel@lists.freepascal.orghttp://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
> My error was caused by the fact that I was using delphi mode - when I used
> the mode switch it worked perfectly :)
>
In mode Delphi you need to use "record helper" for primitive types as
Delphi does not use "type helper" for that.

Regards,
Sven

>
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-04 Thread Gennady Agranov

On 11/4/2018 5:24 PM, Sven Barth via fpc-devel wrote:
Am So., 4. Nov. 2018, 20:36 hat Gennady Agranov 
mailto:gennadyagra...@gmail.com>> geschrieben:


On 11/4/2018 11:24 AM, Sven Barth via fpc-devel wrote:

Am So., 4. Nov. 2018, 12:23 hat Schindler Karl-Michael
mailto:karl-michael.schind...@web.de>> geschrieben:



> Am 04.11.2018 um 12:00 schrieb Ben Grasset
mailto:operato...@gmail.com>>:
>
> From: Ben Grasset mailto:operato...@gmail.com>>
> To: FPC-Devel users discussions
mailto:fpc-devel@lists.freepascal.org>>
> Subject: Re: Array assignment operator
> Message-ID:
mailto:cal4d7fjpfx-yst6z3--fhpr9pts-n47ksfn6m2phd7sunzw...@mail.gmail.com>>
> Content-Type: text/plain; charset="utf-8"
>
> On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov
mailto:gennadyagra...@gmail.com>>
> wrote:
>
>> Hi,
>>
>> Leaving aside the reason why the MiSchi's solution doesn't
work the main
>> question is still not answered :)
>>
>> If you have integer dynamic array "MyArray" is there a way
for the
>> following statement to compile and work correctly:
>>
>> MyArray := 5;
>>
>
> Uh, yes? That's what my example showed.

In your example the length of the array was set to the number
and the elements of the array assigned to their index,
whereas my intention is to keep the length of the array and
fill all elements to the same number.


The operator always creates a new array and does not modify the
existing one.
You'd need to abuse a binary operator (e.g. >< or even <=) for this.

Regards,
Sven



___
fpc-devel maillist  -fpc-devel@lists.freepascal.org

http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


I was thinking that type helper might work - e.g.
MyArray.SetValue(5) and with using properties MyArray.Value := 5

But seems that even simple type helper is not working anymore?

E.g. I was not able to compile example from
http://wiki.freepascal.org/Helper_types :(

TTypeHelper=  type  helperfor  Integer
   procedure  SetZero;
end;

Did you add {$modeswitch typehelpers}?

If so, what is the error you get?

Regards,
Sven



___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


My error was caused by the fact that I was using delphi mode - when I 
used the mode switch it worked perfectly :)


The original problem is solved - at least the way I see it - in FPC you 
can add method to the array :)


Cool!

Thanks!

{$MODESWITCH TYPEHELPERS}

program arrayset;

type
  abc = array of integer;
  abc_helper = type helper for abc
  procedure SetValue(value: integer);
  procedure Print;
end;

procedure abc_helper.SetValue(value: integer);
var
  i: integer;
begin
  for i := Low(self) to High(self) do
    self[i] := value;
end;

procedure abc_helper.Print;
var
  i: integer;
begin
  for i := Low(self) to High(self) do
    Writeln('[',i,'] = ',self[i]);
end;

var
  a: abc;
begin
  SetLength(a,10);
  a.SetValue(5);
  a.Print;
end.

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-04 Thread Sven Barth via fpc-devel
Am So., 4. Nov. 2018, 20:36 hat Gennady Agranov 
geschrieben:

> On 11/4/2018 11:24 AM, Sven Barth via fpc-devel wrote:
>
> Am So., 4. Nov. 2018, 12:23 hat Schindler Karl-Michael <
> karl-michael.schind...@web.de> geschrieben:
>
>>
>>
>> > Am 04.11.2018 um 12:00 schrieb Ben Grasset :
>> >
>> > From: Ben Grasset 
>> > To: FPC-Devel users discussions 
>> > Subject: Re: Array assignment operator
>> > Message-ID: <
>> cal4d7fjpfx-yst6z3--fhpr9pts-n47ksfn6m2phd7sunzw...@mail.gmail.com>
>> > Content-Type: text/plain; charset="utf-8"
>> >
>> > On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov <
>> gennadyagra...@gmail.com>
>> > wrote:
>> >
>> >> Hi,
>> >>
>> >> Leaving aside the reason why the MiSchi's solution doesn't work the
>> main
>> >> question is still not answered :)
>> >>
>> >> If you have integer dynamic array "MyArray" is there a way for the
>> >> following statement to compile and work correctly:
>> >>
>> >> MyArray := 5;
>> >>
>> >
>> > Uh, yes? That's what my example showed.
>>
>> In your example the length of the array was set to the number and the
>> elements of the array assigned to their index, whereas my intention is to
>> keep the length of the array and fill all elements to the same number.
>>
>
> The operator always creates a new array and does not modify the existing
> one.
> You'd need to abuse a binary operator (e.g. >< or even <=) for this.
>
> Regards,
> Sven
>
>>
>
> ___
> fpc-devel maillist  -  
> fpc-devel@lists.freepascal.orghttp://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
> I was thinking that type helper might work - e.g. MyArray.SetValue(5) and
> with using properties MyArray.Value := 5
>
> But seems that even simple type helper is not working anymore?
>
> E.g. I was not able to compile example from
> http://wiki.freepascal.org/Helper_types :(
>
> TTypeHelper = type helper for Integer
>   procedure SetZero;end;
>
> Did you add {$modeswitch typehelpers}?

If so, what is the error you get?

Regards,
Sven

>
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-04 Thread Gennady Agranov

On 11/4/2018 11:24 AM, Sven Barth via fpc-devel wrote:
Am So., 4. Nov. 2018, 12:23 hat Schindler Karl-Michael 
mailto:karl-michael.schind...@web.de>> 
geschrieben:




> Am 04.11.2018 um 12:00 schrieb Ben Grasset mailto:operato...@gmail.com>>:
>
> From: Ben Grasset mailto:operato...@gmail.com>>
> To: FPC-Devel users discussions mailto:fpc-devel@lists.freepascal.org>>
> Subject: Re: Array assignment operator
> Message-ID:
mailto:cal4d7fjpfx-yst6z3--fhpr9pts-n47ksfn6m2phd7sunzw...@mail.gmail.com>>
> Content-Type: text/plain; charset="utf-8"
>
> On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov
mailto:gennadyagra...@gmail.com>>
> wrote:
>
>> Hi,
>>
>> Leaving aside the reason why the MiSchi's solution doesn't work
the main
>> question is still not answered :)
>>
>> If you have integer dynamic array "MyArray" is there a way for the
>> following statement to compile and work correctly:
>>
>> MyArray := 5;
>>
>
> Uh, yes? That's what my example showed.

In your example the length of the array was set to the number and
the elements of the array assigned to their index, whereas my
intention is to keep the length of the array and fill all elements
to the same number.


The operator always creates a new array and does not modify the 
existing one.

You'd need to abuse a binary operator (e.g. >< or even <=) for this.

Regards,
Sven



___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


I was thinking that type helper might work - e.g. MyArray.SetValue(5) 
and with using properties MyArray.Value := 5


But seems that even simple type helper is not working anymore?

E.g. I was not able to compile example from 
http://wiki.freepascal.org/Helper_types :(


TTypeHelper=  type  helperfor  Integer
  procedure  SetZero;
end;

Thanks,

Gennady



___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-04 Thread Sven Barth via fpc-devel
Am So., 4. Nov. 2018, 12:23 hat Schindler Karl-Michael <
karl-michael.schind...@web.de> geschrieben:

>
>
> > Am 04.11.2018 um 12:00 schrieb Ben Grasset :
> >
> > From: Ben Grasset 
> > To: FPC-Devel users discussions 
> > Subject: Re: Array assignment operator
> > Message-ID: <
> cal4d7fjpfx-yst6z3--fhpr9pts-n47ksfn6m2phd7sunzw...@mail.gmail.com>
> > Content-Type: text/plain; charset="utf-8"
> >
> > On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov  >
> > wrote:
> >
> >> Hi,
> >>
> >> Leaving aside the reason why the MiSchi's solution doesn't work the main
> >> question is still not answered :)
> >>
> >> If you have integer dynamic array "MyArray" is there a way for the
> >> following statement to compile and work correctly:
> >>
> >> MyArray := 5;
> >>
> >
> > Uh, yes? That's what my example showed.
>
> In your example the length of the array was set to the number and the
> elements of the array assigned to their index, whereas my intention is to
> keep the length of the array and fill all elements to the same number.
>

The operator always creates a new array and does not modify the existing
one.
You'd need to abuse a binary operator (e.g. >< or even <=) for this.

Regards,
Sven

>
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-04 Thread Schindler Karl-Michael


> Am 04.11.2018 um 12:00 schrieb Ben Grasset :
> 
> From: Ben Grasset 
> To: FPC-Devel users discussions 
> Subject: Re: Array assignment operator
> Message-ID: 
> 
> Content-Type: text/plain; charset="utf-8"
> 
> On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov 
> wrote:
> 
>> Hi,
>> 
>> Leaving aside the reason why the MiSchi's solution doesn't work the main
>> question is still not answered :)
>> 
>> If you have integer dynamic array "MyArray" is there a way for the
>> following statement to compile and work correctly:
>> 
>> MyArray := 5;
>> 
> 
> Uh, yes? That's what my example showed.

In your example the length of the array was set to the number and the elements 
of the array assigned to their index, whereas my intention is to keep the 
length of the array and fill all elements to the same number.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-03 Thread Ben Grasset
On Sat, Nov 3, 2018 at 4:44 PM Gennady Agranov 
wrote:

> Hi,
>
> Leaving aside the reason why the MiSchi's solution doesn't work the main
> question is still not answered :)
>
> If you have integer dynamic array "MyArray" is there a way for the
> following statement to compile and work correctly:
>
> MyArray := 5;
>

Uh, yes? That's what my example showed.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-03 Thread Gennady Agranov

Hi,

Leaving aside the reason why the MiSchi's solution doesn't work the main 
question is still not answered :)


If you have integer dynamic array "MyArray" is there a way for the 
following statement to compile and work correctly:


MyArray := 5;

Thanks,
Gennady


On 11/3/2018 4:14 PM, Ben Grasset wrote:
On Sat, Nov 3, 2018 at 8:01 AM Schindler Karl-Michael 
mailto:karl-michael.schind...@web.de>> 
wrote:


Hi

I would like to use a simple assignment operator for arrays, with
which all elements of an array are assigned to the same value,
similar to an extended initialize, not to zero but to a value of
choice. A simple example for an integer array would be:

  myArray := 5;

With arrays with defined bounds, it works, but i was not able to
do it with a dynamic array. My test case is this:

program arrayAssign;

type
  TmyArray = array of integer;

var
  myArray: TmyArray;
  index: integer;

operator := (const number: integer) theResult: TmyArray;
  var
    i: integer;
  begin
    for i := low(theResult) to high(theResult) do
      theResult[i] := number;
  end;

begin
  setlength(myArray, 10);
  writeln ('myArray: ', low(myArray), ', ', high(myArray));

  myArray := 5;

  writeln ('myArray: ', low(myArray), ', ', high(myArray));
  for index := low(myArray) to high(myArray) do
    writeln (index, ': ', myArray[index]);
end.

The output is:

myArray: 0, 9
myArray: 0, -1

The problem is that in the declaration of the operator, the
functions low and high seem to return the values of the type
TmyArray, i.e. 0 and -1 and not the values of the variable of the
assignment statement, i.e. myArray and the assignment nullifies
the previous setlength. Is there any way around this and obtain
the actual values of myArray?

Michael, aka MiSchi.
___
fpc-devel maillist  - fpc-devel@lists.freepascal.org

http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


There's two issues:
1) The result of the operator is always a new, unique array (as in 
it's unrelated to the myArray variable)
2) Your operator doesn't initialize the result with a length, so it's 
just iterating over nothing.


Here's an example of the correct way to write the operator and use it:

program ArrayOverloads;

{$mode ObjFPC}

type TIntArray = array of Integer;

  operator := (const I: Integer): TIntArray; inline;
  var V: Integer;
  begin
    SetLength(Result, I);
    for V := 0 to Pred(I) do Result[V] := V;
  end;

var
  I: Integer;
  IA: TIntArray;

begin
  IA := 25;
  for I in IA do Write(I, ' ');
  WriteLn;
  for I := High(IA) downto 0 do Write(I, ' ');
  WriteLn;
  for I := Low(IA) to High(IA) do Write(I, ' ');
  WriteLn;
  I := 0;
  while I < Pred(High(IA)) do begin
    Inc(I, 2);
    Write(I, ' ');
  end;
end.

Output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
2 4 6 8 10 12 14 16 18 20 22 24


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel



___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Array assignment operator

2018-11-03 Thread Ben Grasset
On Sat, Nov 3, 2018 at 8:01 AM Schindler Karl-Michael <
karl-michael.schind...@web.de> wrote:

> Hi
>
> I would like to use a simple assignment operator for arrays, with which
> all elements of an array are assigned to the same value, similar to an
> extended initialize, not to zero but to a value of choice. A simple example
> for an integer array would be:
>
>   myArray := 5;
>
> With arrays with defined bounds, it works, but i was not able to do it
> with a dynamic array. My test case is this:
>
> program arrayAssign;
>
> type
>   TmyArray = array of integer;
>
> var
>   myArray: TmyArray;
>   index: integer;
>
> operator := (const number: integer) theResult: TmyArray;
>   var
> i: integer;
>   begin
> for i := low(theResult) to high(theResult) do
>   theResult[i] := number;
>   end;
>
> begin
>   setlength(myArray, 10);
>   writeln ('myArray: ', low(myArray), ', ', high(myArray));
>
>   myArray := 5;
>
>   writeln ('myArray: ', low(myArray), ', ', high(myArray));
>   for index := low(myArray) to high(myArray) do
> writeln (index, ': ', myArray[index]);
> end.
>
> The output is:
>
> myArray: 0, 9
> myArray: 0, -1
>
> The problem is that in the declaration of the operator, the functions low
> and high seem to return the values of the type TmyArray, i.e. 0 and -1 and
> not the values of the variable of the assignment statement, i.e. myArray
> and the assignment nullifies the previous setlength. Is there any way
> around this and obtain the actual values of myArray?
>
> Michael, aka MiSchi.
> ___
> fpc-devel maillist  -  fpc-devel@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


There's two issues:
1) The result of the operator is always a new, unique array (as in it's
unrelated to the myArray variable)
2) Your operator doesn't initialize the result with a length, so it's just
iterating over nothing.

Here's an example of the correct way to write the operator and use it:

program ArrayOverloads;

{$mode ObjFPC}

type TIntArray = array of Integer;

  operator := (const I: Integer): TIntArray; inline;
  var V: Integer;
  begin
SetLength(Result, I);
for V := 0 to Pred(I) do Result[V] := V;
  end;

var
  I: Integer;
  IA: TIntArray;

begin
  IA := 25;
  for I in IA do Write(I, ' ');
  WriteLn;
  for I := High(IA) downto 0 do Write(I, ' ');
  WriteLn;
  for I := Low(IA) to High(IA) do Write(I, ' ');
  WriteLn;
  I := 0;
  while I < Pred(High(IA)) do begin
Inc(I, 2);
Write(I, ' ');
  end;
end.

Output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
2 4 6 8 10 12 14 16 18 20 22 24
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel