Jump to content
Tommi Prami

Array of named and typed parameters. (My Mind is blank)

Recommended Posts

After couple o  sleepless night my brain is shutting down.

 

I need to do something like this.

Foo(
  [
    [1, 'First', 0.01],
    [2, 'Second', 0.02],
    [3, 'Third', 0.03]
  ])

So array of array of named and typed parameters. Think I've done something like this.  In the  method itself I could do something like

if AParameters[1].IntValue = 2 then ....

in Other words What I am trying to avoid is the I should do some kind of initialization of that array

SetLEnght(LArray, 3);
LArray[0].IntValue := 1;
...
Foo(LArray);

 

-Tee-

Edited by Tommi Prami

Share this post


Link to post

Open array of record, using a function or record constructor to populate new instances of that record. 

 

Syntax support for initialization is a bit crappy in Delphi sadly. 

Edited by David Heffernan
  • Like 1

Share this post


Link to post
2 minutes ago, David Heffernan said:

Open array of record, using a function or record constructor to populate new instances of that record. 

I was thinking that Record must be used in this...

Quote

Syntax support for initialization is a bit crappy in Delphi sadly. 

Sadly yes. 

 

That kind of constant one liner calling like that would be cool. Sometimes it is just handy.  

Share this post


Link to post

 

1 hour ago, Tommi Prami said:

I need to do something like this.


Foo(
  [
    [1, 'First', 0.01],
    [2, 'Second', 0.02],
    [3, 'Third', 0.03]
  ])
Foo(
  [
    Bar([1, 'First', 0.01]),
    Bar([2, 'Second', 0.02]),
    Bar([3, 'Third', 0.03])
  ])

where Bar is function-"constructor"

or, in newer Delphis, you can use inline variable inits

Edited by Fr0sT.Brutal

Share this post


Link to post
29 minutes ago, Fr0sT.Brutal said:

or, in newer Delphis, you can use inline variable inits

Please no... 😄 Even at work we agreed that we'll not use inline variable declarations when we upgraded to 10.3.3. In my humble opinion this feature was utterly useless and will make Pascal code harder to read - at least for me, that is.

  • Like 3

Share this post


Link to post

Here is a variation I use - I like type safety.

type
  Setting = record
    i: integer;
    s: string;
    d: double;
    constructor Create(const _i: integer; _s: string; _d:double);
  end;

  Option = record const
    One: Setting =    (i:1; s:'First';  d:0.01);
    Two: Setting =    (i:2; s:'Second'; d:0.02);
    Three: Setting =  (i:3; s:'Third';  d:0.03);
    class function Add(const _i: integer; _s: string; _d:double): Setting; static; // Sugar
  end;

constructor Setting.Create(const _i: integer; _s: string; _d: double);
begin
  i := _i;
  s := _s;
  d := _d;
end;

class function Option.Add(const _i: integer; _s: string; _d: double): Setting;
begin
  Result := Setting.Create(_i, _s, _d);
end;

procedure Foo(const Options: TArray<Setting>);
begin
  for var Option: Setting in Options
   do if Option.i in [2, 4]
    then Writeln(Format('i:%d s:%s d:%f', [Option.i, Option.s, Option.d]));
end;

procedure TestFoo;
begin
  Foo([Option.One, Option.Two, Option.Three, Option.Add(4,'Fourth',0.04)]);
end;

 

  • Like 1

Share this post


Link to post
7 hours ago, aehimself said:

Please no... 😄 Even at work we agreed that we'll not use inline variable declarations when we upgraded to 10.3.3. In my humble opinion this feature was utterly useless and will make Pascal code harder to read - at least for me, that is.

Well, for the purpose of bringing global var/const init capabilities into the code section they could serve quite well at the same time not causing too much eye pain to oldschool guys 😉

Foo(var r: TSomeRec = 
  (
	fInt: 123;
	fStr: 'bar'
  ))

(at least as I imagine it - haven't tried 10.3 yet)

Share this post


Link to post
2 hours ago, Fr0sT.Brutal said:

[...] not causing too much eye pain to oldschool guys 😉

Wow, I'm not even 35 yet and being called oldschool 😞 I guess "OK boomer" is next in my bucket list 😄 

Share this post


Link to post

Being oldschool is not tightly connected with age 😉 moreover, in today IT world, one could be called oldschool just for using 1-year-old techniques )))

Share this post


Link to post
32 minutes ago, Fr0sT.Brutal said:

[...] one could be called oldschool just for using 1-year-old techniques )))

This is something I really did not consider, and you could not be more right.

Especially if you speak the trendy JavaScript as new libraries (frameworks, my bad) are popping up like mushrooms.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×