Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-07

SetLength procedure

9 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Jürgen Hestermann
to Steve Gatenby
Steve Gatenby wrote
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);
Steve Gatenby
to Jürgen Hestermann

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

Jürgen Hestermann wrote 21 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
leledumbo
to Steve Gatenby
Show quoted text 11 lines
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 .
Steve Gatenby
to leledumbo

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

leledumbo wrote 43 lines
> 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 . _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Jürgen Hestermann
to Steve Gatenby: It is only a personal preference, not a serious problem. I would find code in the form of…
Steve Gatenby wrote 6 lines
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.

Andreas Berger
to Jürgen Hestermann

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.

Jürgen Hestermann wrote 32 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Jürgen Hestermann
to Andreas Berger
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. 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.

Saunders, Rich
to Andreas Berger: I very seldom comment on this, or any, forum unless I have a good answer and no one else…
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