Recent

Author Topic: Indexed properties  (Read 1469 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 3289
Indexed properties
« on: September 15, 2021, 10:10:13 pm »
I appear to have successfully defined a property indexed by a const string: can anybody confirm that that's intentional and safe?

Code: Pascal  [Select][+][-]
  1. TSerialThread= class(TThread)
  2.   strict private
  3. ...
  4.     function getStationState(const address: string): TStationState;
  5.     procedure setStationState(const address: string; state: TStationState);
  6.   public
  7. ...
  8.     property StationState[const address: string]: TStationState
  9.                                         read getStationState write setStationState;
  10. end;
  11.  

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Awkward

  • Jr. Member
  • **
  • Posts: 95
Re: Indexed properties
« Reply #1 on: September 15, 2021, 10:20:48 pm »
why it must be unsafe? i used similar props long time ago (for ini files). ok, ok, i used with string in property define (without const) but with it in getter/setter

simone

  • Sr. Member
  • ****
  • Posts: 408
Re: Indexed properties
« Reply #2 on: September 16, 2021, 12:00:07 am »
I think that the property in your code is an array property, not an indexed property.
Microsoft Windows 10 64 bit - Lazarus 2.0.12

Warfley

  • Hero Member
  • *****
  • Posts: 561
Re: Indexed properties
« Reply #3 on: September 16, 2021, 12:51:37 am »
I mean it is used for years (possibly decades) in the RTL:
Code: Pascal  [Select][+][-]
  1.   TStrings = class(TPersistent)
  2.     [...]
  3.     function GetValue(const Name: string): string;
  4.     [...]
  5.     procedure SetValue(const Name, Value: string);
  6.     [...]
  7.     property Values[const Name: string]: string read GetValue write SetValue;
  8.     [...]
  9.   end;
So i think this is intentional. Also, with a compiler switch (i forgot which one) you can also use constref (I once made a topic, probably findable via the search or google)
And you can use any type here, not only strings, you can use classes, records, pointers, etc. TDictionary or TMap uses this for example with  a generic type
« Last Edit: September 16, 2021, 12:53:13 am by Warfley »

MarkMLl

  • Hero Member
  • *****
  • Posts: 3289
Re: Indexed properties
« Reply #4 on: September 16, 2021, 08:21:29 am »
I mean it is used for years (possibly decades) in the RTL:
...
So i think this is intentional. Also, with a compiler switch (i forgot which one) you can also use constref (I once made a topic, probably findable via the search or google)
And you can use any type here, not only strings, you can use classes, records, pointers, etc. TDictionary or TMap uses this for example with  a generic type

Thanks for that. I'd previously assumed it had to be an integer (etc.) but this is very much what I need in the current case.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 3421
  • Compiler Developer
Re: Indexed properties
« Reply #5 on: September 16, 2021, 09:06:24 am »
I appear to have successfully defined a property indexed by a const string: can anybody confirm that that's intentional and safe?

Yes, they are. They are called Array Properties. Please note that Indexed Properties are something different.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3289
Re: Indexed properties
« Reply #6 on: September 16, 2021, 09:20:34 am »
Yes, they are. They are called Array Properties. Please note that Indexed Properties are something different.

Thanks, noted. Just being cautious.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018