[fpc-devel] [] property overloads

Ben Grasset operator97 at gmail.com
Tue Jul 9 23:32:54 CEST 2019


On Tue, Jul 9, 2019 at 4:18 PM Ryan Joseph <genericptr at gmail.com> wrote:

> it goes against the idea of properties anyways.
>

I don't know if I completely agree with this part in a general sense.
There's plenty of perfectly valid uses for properties that are nothing like
the straightforward "make something act like an array" stuff:

program Example;

{$mode Delphi}

uses SysUtils;

type
  StringGetter = function: String;
  function my: String; begin Exit('my'); end;
  function favorite: String; begin Exit('favorite'); end;
  function color: String; begin Exit('color'); end;
  function &is: String; begin Exit('is'); end;
  function red: String; begin Exit('red'); end;
  function green: String; begin Exit('green'); end;
  function blue: String; begin Exit('blue'); end;

type
  SentenceBuilder = record
  strict private
    class function GetSentence(constref A: array of StringGetter): String;
static;
  public
    class property Sentence[constref A: array of StringGetter]: String read
GetSentence; default;
  end;

  class function SentenceBuilder.GetSentence(constref A: array of
StringGetter): String;
  var Fn: StringGetter;
  begin
    Result := '';
    for Fn in A do Result := Result + Fn() + ' ';
    Result := Result.TrimRight() + '.';
    Result[1] := UpCase(Result[1]);
  end;

begin
  WriteLn(SentenceBuilder[[
    My,
    favorite,
    color,
    &is,
    red
  ]]);
  WriteLn(SentenceBuilder[[
    Blue,
    &is,
    my,
    favorite,
    color
  ]]);
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190709/90ace4ff/attachment.html>


More information about the fpc-devel mailing list