TArray.Create<T> method

Issue #257 wontfix
Zigmund Bulinsh created an issue

Hi!

Would be nice to have the following method in Spring.pas: function TArray.Create<T>(const AValues: TArray<T>): TArray<T>; begin Result := AValue; end;

From a first look maybe it looks useless, but when it comes to constant dynamic array which were added in XE7 (if I am not wrong) this makes more sense.

So the case is when I want to set certain properties say for multiple objects i can create a method like (for example): procedure SetHint(const AControl: TArray<TButton>); And I can call it like SetHint([Button1, Button2]). Or other solution is to define the local variable "LButtons" and then assign it the [Button1, Button2] and then do the FOR IN statement.

But with TArray.Create we can do it in the shortest way!

for LButton in TArray.Create<TButton>([Button1, Button2]) do

where Delphi is not yet so smart to allow (for known reasons): for LButton in [Button1, Button2] do

The example is artificial, of course it's just about using the constant dynamic array immediatly for the for in loop or other places where you need enumerable etc.

Thanks for your time.

Comments (4)

  1. Stefan Glienke repo owner

    This is already possible (with a little overhead though):

    for LButton in TArray.Copy([Button1, Button2]) do
    
  2. Zigmund Bulinsh reporter

    Thanks for the hint. But as you mention it's an overhead a bit. Especially if the element of the array are the records of bigger sizes. For me it's fine as I just created the function for that where I need it. And the other thing of the TArray.Copy() is the way how we read it - not really natural speaking - but that's again a matter of taste.

    So if you see that there is no need in this method (Create<> or From<> - whatever it can be called) we can close the topic :)

    Thank you again for the beautiful library!

  3. Log in to comment