Recent

Author Topic: string argument as array index  (Read 1255 times)

Weiss

  • Full Member
  • ***
  • Posts: 214
string argument as array index
« on: May 20, 2025, 05:56:16 pm »
I was thinking to implement a simple analogue of TDictionary, and all works remarkably well. For my needs, I get-by with a class where TPair is emulated with parallel arrays. One thing I can't emulate is placing a string as an argument in a square brackets. And with that being not done, I have to replace a syntax in many  lines of existing code. Not big of a deal, but I am curious, how is it done? Say, I have a key "myKey", how in free pascal I can do
Code: Pascal  [Select][+][-]
  1. ..
  2. someVariable :=myHomeMadeDictionary[myKey]
  3. ..
  4.  

sorry for such "insightful" question
 

Thaddy

  • Hero Member
  • *****
  • Posts: 17198
  • Ceterum censeo Trump esse delendam
Re: string argument as array index
« Reply #1 on: May 20, 2025, 06:20:16 pm »
I do not understand the question: generics are basically a sourcecode replay and a fill in. That is codewise not efficient, but anything else, because the compiler will generate code for every specialization, your "insightfull" part is even worse.

Generics do not loose speed. Inheritance does not loose speed, but your suggestion does loose speed.

A small example of being blind to what is really happening under the hood.
« Last Edit: May 20, 2025, 06:25:33 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

cdbc

  • Hero Member
  • *****
  • Posts: 2217
    • http://www.cdbc.dk
Re: string argument as array index
« Reply #2 on: May 20, 2025, 06:21:42 pm »
Hi
It's got to do with properties...
Have a look in the attached unit, it's just a quick mockup, but you should get it...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 872
Re: string argument as array index
« Reply #3 on: May 20, 2025, 06:22:52 pm »
I guess, this can be done via default array property. Like this one:
Code: Pascal  [Select][+][-]
  1.       function GetItem (AKey:TKey):TValue;
  2.       procedure SetItem(AKey:TKey;AValue:TValue);
  3.       property Items[AIndex:TKey]:TValue read GetItem write SetItem;default;
  4.  
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 17198
  • Ceterum censeo Trump esse delendam
Re: string argument as array index
« Reply #4 on: May 20, 2025, 06:23:16 pm »
Hi
It's got to do with properties...
Have a look in the attached unit, it's just a quick mockup, but you should get it...
Regards Benny
Benny, you are wrong here. Read my answer: he thinks he can speed up code that way and that is wrong.

The recent Pascal syntax is so rich it can be done but it will be slower.
« Last Edit: May 20, 2025, 06:26:02 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: string argument as array index
« Reply #5 on: May 20, 2025, 06:27:02 pm »
I use TCollection
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

cdbc

  • Hero Member
  • *****
  • Posts: 2217
    • http://www.cdbc.dk
Re: string argument as array index
« Reply #6 on: May 20, 2025, 06:29:11 pm »
Hi Thaddy
I saw that, but decided to answer the question about default props, just like Mr.Madguy...
His fiddling with this, is his business, I know exactly when and where to use generics  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: string argument as array index
« Reply #7 on: May 20, 2025, 06:50:26 pm »
Maybe procedural properties also support [] ?

https://www.freepascal.org/docs-html/current/ref/refse27.html

Weiss

  • Full Member
  • ***
  • Posts: 214
Re: string argument as array index
« Reply #8 on: May 20, 2025, 06:52:47 pm »
Hi
It's got to do with properties...
Have a look in the attached unit, it's just a quick mockup, but you should get it...
Regards Benny

great! Thank you sir


PascalDragon

  • Hero Member
  • *****
  • Posts: 6008
  • Compiler Developer
Re: string argument as array index
« Reply #9 on: May 22, 2025, 09:31:32 pm »
Hi
It's got to do with properties...
Have a look in the attached unit, it's just a quick mockup, but you should get it...
Regards Benny
Benny, you are wrong here. Read my answer: he thinks he can speed up code that way and that is wrong.

Nowhere did Weiss mention anything about optimization, so the suggestions by both cdbc and Mr.Madguy are indeed the correct ones (and can also be used together with generics if necessary).

jcmontherock

  • Sr. Member
  • ****
  • Posts: 299
Re: string argument as array index
« Reply #10 on: May 23, 2025, 11:38:26 am »
You can also use TStringList Name Values with objects. So a lot of work is done by Pascal.
Windows 11 UTF8-64 - Lazarus 4.0-64 - FPC 3.2.2

Weiss

  • Full Member
  • ***
  • Posts: 214
Re: string argument as array index
« Reply #11 on: May 24, 2025, 08:24:10 pm »
TStringList was on my mind, thank you jcmontherock. And Marcov, great lead.

To the question why, as I see it gets some negative reaction, I have a lot to say. For one, code writing is my hobby, I did not slow down anybody's project or used up resources anywhere, other than family time.

I get about 40 warnings from Generics.Collections. Code compiles and works, but those warnings lead me to feeling that there is some heywiring under the hood. Maybe nothing important.

There was saying somewhere, with OOP if you want banana you are getting it with guerilla holding that banana and the whole jungle behind. I only need small portion of Generics, seemed logical to write that portion.

jamie

  • Hero Member
  • *****
  • Posts: 6964
Re: string argument as array index
« Reply #12 on: May 24, 2025, 08:26:49 pm »
Look at the Fpg unit, it contains Generics that is much lighter than the suggested collections and from my experience actually makes the compiler happier.

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 17198
  • Ceterum censeo Trump esse delendam
Re: string argument as array index
« Reply #13 on: May 25, 2025, 04:38:50 pm »
Generics.Collections is Delphi compatible. FGL is not! But you are correct, it is lightweight.
Also note the confusion that always seems to exist: some think a dictionary <> map, while dictionary = map.
Fgl and generics.collections are not compatible but contain some of the same functionality.

For anything that translates from or needs Delphi: always use generics.collections.
For much of the code you only use in FreePascal? feel free to use the fgl.
I have another strong favorite, lgenerics by avk, but that is not in the standard distribution.

From a CS point of view it is best.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jamie

  • Hero Member
  • *****
  • Posts: 6964
Re: string argument as array index
« Reply #14 on: May 25, 2025, 05:25:23 pm »
Generics.Collections is Delphi compatible. FGL is not! But you are correct, it is lightweight.
Also note the confusion that always seems to exist: some think a dictionary <> map, while dictionary = map.
Fgl and generics.collections are not compatible but contain some of the same functionality.

For anything that translates from or needs Delphi: always use generics.collections.
For much of the code you only use in FreePascal? feel free to use the fgl.
I have another strong favorite, lgenerics by avk, but that is not in the standard distribution.

From a CS point of view it is best.

That's fine and dandy however, serious use of the Delphi compatible collections generics, crashes the compiler!

On top of that, it's not well behaved like those promoting it, think.

 I will say however, it seems to work in Delphi's environment and after looking at some code snippets they are not actually the same as implemented in the
supported unit for fpc. So, who's so say where the real problem is.

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018