Tommi Prami 130 Posted February 21, 2020 (edited) 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 February 21, 2020 by Tommi Prami Share this post Link to post
David Heffernan 2345 Posted February 21, 2020 (edited) 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 February 21, 2020 by David Heffernan 1 Share this post Link to post
Tommi Prami 130 Posted February 21, 2020 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
Fr0sT.Brutal 900 Posted February 21, 2020 (edited) 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 February 21, 2020 by Fr0sT.Brutal Share this post Link to post
aehimself 396 Posted February 21, 2020 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. 3 Share this post Link to post
Lars Fosdal 1792 Posted February 21, 2020 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; 1 Share this post Link to post
Fr0sT.Brutal 900 Posted February 21, 2020 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
aehimself 396 Posted February 21, 2020 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
Fr0sT.Brutal 900 Posted February 25, 2020 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
aehimself 396 Posted February 25, 2020 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