SetLength procedure

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

SetLength procedure

Steve Gatenby
Could anybody enlighten me on how to make the 'SetLength' procedure into
an equivalent function

I have many instances of
   SetLength(xxx, Length(xxx)+1);
   Num := High(xxx);

I would like to have Num :=  SetLength(xxx, Length(xxx)+1);
  or even something like Num := IncLength(xxx, 1);

These are on varying types of array, so not really feasible to create a
function for each one.
Hoping it is possible within pascal itself :)

Thanks - SteveG
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Jürgen Hestermann

Am 2014-07-27 12:10, schrieb Steve Gatenby:
 > Could anybody enlighten me on how to make the 'SetLength' procedure into an equivalent function
 > I have many instances of
 >   SetLength(xxx, Length(xxx)+1);
 >   Num := High(xxx);


What is the problem with this code?
Why force these 2 commands into a function?
Just to save a little bit of typing?
When *reading* this code it is much
clearer what it does as when reading

Num := IncLength(xxx, 1);


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Steve Gatenby
It is only a personal preference, not a serious problem.

I would find code in the form of
   ArrayLen := IncLength(ArrayName, 1);

to be much more readable then

    SetLength(ArrayName, Length(ArrayName)+1);
    ArrayLen:= High(ArrayName);

I was just hoping somebody may know how its done.

Thanks - SteveG

On 27/07/14 20:09, Jürgen Hestermann wrote:

>
> Am 2014-07-27 12:10, schrieb Steve Gatenby:
> > Could anybody enlighten me on how to make the 'SetLength' procedure
> into an equivalent function
> > I have many instances of
> >   SetLength(xxx, Length(xxx)+1);
> >   Num := High(xxx);
>
>
> What is the problem with this code?
> Why force these 2 commands into a function?
> Just to save a little bit of typing?
> When *reading* this code it is much
> clearer what it does as when reading
>
> Num := IncLength(xxx, 1);
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

leledumbo
Administrator
> It is only a personal preference, not a serious problem.
>
> I would find code in the form of
>    ArrayLen := IncLength(ArrayName, 1);
>
> to be much more readable then
>
>     SetLength(ArrayName, Length(ArrayName)+1);
>     ArrayLen:= High(ArrayName);
>
> I was just hoping somebody may know how its done.

Without support for generic function, currently you have to use generic class method:

type
  generic TDynArrayUtil<T> = class
  public
    class function SetLength(var a: T; const n: Integer): Integer;
  end;

class function TDynArrayUtil.SetLength(var a: T; const n: Integer): Integer;
begin
  System.SetLength(a,Length(a) + n);
  Result := High(a);
end;

and use it like:

var
  a: TIntegerDynArray;
...
maxn := specialize TDynArrayUtil<TIntegerDynArray>.SetLength(a,1);

note that you have to instantiate with the correct type.
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Steve Gatenby
Thanks for that - I will give it a try :)


On 27/07/14 21:21, leledumbo wrote:

>> It is only a personal preference, not a serious problem.
>>
>> I would find code in the form of
>>     ArrayLen := IncLength(ArrayName, 1);
>>
>> to be much more readable then
>>
>>      SetLength(ArrayName, Length(ArrayName)+1);
>>      ArrayLen:= High(ArrayName);
>>
>> I was just hoping somebody may know how its done.
> Without support for generic function, currently you have to use generic
> class method:
>
> type
>    generic TDynArrayUtil<T> = class
>    public
>      class function SetLength(var a: T; const n: Integer): Integer;
>    end;
>
> class function TDynArrayUtil.SetLength(var a: T; const n: Integer): Integer;
> begin
>    System.SetLength(a,Length(a) + n);
>    Result := High(a);
> end;
>
> and use it like:
>
> var
>    a: TIntegerDynArray;
> ...
> maxn := specialize TDynArrayUtil<TIntegerDynArray>.SetLength(a,1);
>
> note that you have to instantiate with the correct type.
>
>
>
> --
> View this message in context: http://free-pascal-general.1045716.n5.nabble.com/SetLength-procedure-tp5719807p5719810.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Jürgen Hestermann
In reply to this post by Steve Gatenby

Am 2014-07-27 12:51, schrieb Steve Gatenby:
> I would find code in the form of
>   ArrayLen := IncLength(ArrayName, 1);
> to be much more readable then
>    SetLength(ArrayName, Length(ArrayName)+1);
>    ArrayLen:= High(ArrayName);
>

Why? It is not clear which index ArrayLen will receive from IncLength.
You need to look up (or remember) what exactly this function does
while in the two-line version it is all clear and unambiguous.
Especially, if you not only add 1 element but more as in

   ArrayLen := IncLength(ArrayName,2);

    SetLength(ArrayName, Length(ArrayName)+2);
    ArrayLen:= High(ArrayName);

In the first version, which value will ArrayLen be set to?
High(ArrayName) or High(ArrayName)-1?
This is very clear in the second version.

IMO it is not worth investing time into writing a function
that obscures the code. If you look at it months later
you may no longer know what IncLength does exactly,
not to talk about if someone else has/wants to read
your code.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Andreas Berger
I very seldom comment on this, or any, forum unless I have a good answer
and no one else seem to have one. This case is different. I have noticed
quit often here that instead of answering the question people try to
reason why in the world the author of the question would even want to do
this. Would it not be better to tell him how to do it or at the most say
"I don't know". I know there are times when you want to prevent someone
from making a serious mistake, but this is not the case here.
Sometimes the question is placed by someone new to programming and it is
important to learn from your own work, mistakes and all. Often the
question also comes from people migrating from, or knowledgeable with,
another language where a similar functionality exits. This type of
argument will only discourage them away from our beloved Pascal.

Nur mein Senf.

On Sun 27/07/2014 11:18, Jürgen Hestermann wrote:

>
> Am 2014-07-27 12:51, schrieb Steve Gatenby:
>> I would find code in the form of
>>   ArrayLen := IncLength(ArrayName, 1);
>> to be much more readable then
>>    SetLength(ArrayName, Length(ArrayName)+1);
>>    ArrayLen:= High(ArrayName);
>>
>
> Why? It is not clear which index ArrayLen will receive from IncLength.
> You need to look up (or remember) what exactly this function does
> while in the two-line version it is all clear and unambiguous.
> Especially, if you not only add 1 element but more as in
>
>   ArrayLen := IncLength(ArrayName,2);
>
>    SetLength(ArrayName, Length(ArrayName)+2);
>    ArrayLen:= High(ArrayName);
>
> In the first version, which value will ArrayLen be set to?
> High(ArrayName) or High(ArrayName)-1?
> This is very clear in the second version.
>
> IMO it is not worth investing time into writing a function
> that obscures the code. If you look at it months later
> you may no longer know what IncLength does exactly,
> not to talk about if someone else has/wants to read
> your code.
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Jürgen Hestermann

Am 2014-07-27 19:28, schrieb Andreas Berger:
> I have noticed quit often here that instead of answering the question people try to reason why in the world the author of the question would even want to do this. Would it not be better to tell him how to do it or at the most say "I don't know". I know there are times when you want to prevent someone from making a serious mistake, but this is not the case here.

I know what you mean but in this case I find it worth mentioning that the 2 line code *is* easier to read.
And for me it would be wasting time to somehow find a solution for his problem instead of simply
using the 2 line code and use the saved time for real problems.
The 2 line code is definitely more in the spirit of Pascal than
obscuring what happens with a (not so clear) function.

But of course, everybody can do what he wants.
It was just meant in the sense of an advice.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SetLength procedure

Saunders, Rich
In reply to this post by Andreas Berger
On 2014-07-27 13:28, Andreas Berger wrote:
> I have noticed quit often here that instead of answering the question
> people try to reason why in the world the author of the question would
> even want to do this.

I agree that this is an annoying pattern on this list.

I would suggest everyone at least wait until someone tries to respond
with an actual answer to the question before diverting discussion in
this way.

--

Cheers!
Rich Saunders
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal