Welcome, Guest
Username: Password: Remember me
General Purpose Components and Libraries, discussions, problems and suggestions
  • Page:
  • 1

TOPIC:

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3131

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
Can FPC have default class properties in Delphi mode ?

Please Log in or Create an account to join the conversation.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3133

  • Konstantinos Papadoulas
  • Konstantinos Papadoulas's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 131
  • Thank you received: 19
Hi Arioch,

I have a class, in one of my programs, with a "Default" keyword after the property's declaration:
  property Code[nIndex: Word]:TIRData  Read GetCode  Write SetCode; default;

This helps me to write something like:
  MyClass[2] := <Variable of TIRData>;

instead of:
  MyClass.Code[2] := <Variable of TIRData>;

Is this what you wanted to justify in the first place or something else (like a "class var")?

Kostas

Please Log in or Create an account to join the conversation.

Last edit: by Konstantinos Papadoulas.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3135

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
that is a regular property, like TStrings.Strings and TList.Items - it only works against instances(objects)

i want the same but wrt the class

like

> class property Code[nIndex: Word]:TIRData Read GetCode; default;

Please Log in or Create an account to join the conversation.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3136

  • Konstantinos Papadoulas
  • Konstantinos Papadoulas's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 131
  • Thank you received: 19
Ohhh.. now i see..

Check this out:
Extended Class Syntax

...hopefully this will help you.

Kostas

Please Log in or Create an account to join the conversation.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3138

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
no it does not, i was there. It is very short - more like short notes on the sleeve.

In Delphi that property is prohibited to be default. Such a limitation may happen in FPC, but not documented...

Please Log in or Create an account to join the conversation.

Last edit: by Arioch.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3140

  • Konstantinos Papadoulas
  • Konstantinos Papadoulas's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 131
  • Thank you received: 19
then plase do me a favor. Post here an implementation of the class you want to make using the keywords "class" and "default" wherever
you want them even if the code you will write will not compile.

In order to get a "screenshot" of your thoughts for the type of class you want to build.

Kostas

Please Log in or Create an account to join the conversation.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3142

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
i probably won't really do it, for my major concern is still Delphi, and that makes a special note that "defaul" and "class" are mutually exclusive.

Pity... i wished to do a light-weight factory (which Delphi class references are to an extent). I would have to resort to global procedure, but that is less elegant.
And calling TClassName.a(DataPayload) is awful. TClassName[DataPayload] would be neat...

Please Log in or Create an account to join the conversation.

Last edit: by Arioch.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3143

  • Konstantinos Papadoulas
  • Konstantinos Papadoulas's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 131
  • Thank you received: 19
So i guess that you have allready made some tests in FreePascal yourself before posting here.
Maybe i'll try it myself if i find the time to see if it is possible to do it in a simple class implementation
and if i find a solution then to try it in a more complex class.

Too bad that this functionality will keep you away from an interesting implementation of your work in FPC.

Kostas.

Please Log in or Create an account to join the conversation.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3144

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
not yet, it is still rebuilding from 2.8 to 3.1

but i am total noob in FPC and i can easily miss some knob to turn.

For example i almost only by chance figured out how to compile function Foo(const Foo: integer): integer;
Oversite on my part, but it worked like charm in Delphi :-)

PS. While i think all that ... stuff ... that happened around XE3 would make next wave of Delphi refugees and many of them would end in FPC or Oxygene realms, but i still consider Delphi a mainstream. And yes, i saw latest TIOBE :-)

Please Log in or Create an account to join the conversation.

Last edit: by Arioch.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3146

  • Konstantinos Papadoulas
  • Konstantinos Papadoulas's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 131
  • Thank you received: 19
finally (even if it's too late) i have found time for tests on default class propety.

i will post here soon.

Kostas.

OK. A simple test is done. Unfortunatelly no luck...

The following code is the class declaration and is compiled successfully:
TTestClass = class
private
  //variables
  class var FCProp: Array [1..4] of Integer;
  //funcs & procs
  class function GetCProp(nIndex: Word): Integer; static;
  class procedure SetCProp(nIndex: Word; AValue: Integer); static;
public
  class property CProp[nIndex: Word]: Integer Read GetCProp Write SetCProp; default;
end;

...and the following is the code that calls the default class property and it does
not compile:
  memo1.Lines.Add('Get TTestClass[1]: ' + IntToStr(TTestClass[1]));

so we need to fall back to the normal implementation:
  TestClass := TTestClass.Create;
  TestClass[1] := 34;
  memo1.Lines.Add('Get TestClass[1]: ' + IntToStr(TestClass[1]));
  TestClass.Free;

Please Log in or Create an account to join the conversation.

Last edit: by Konstantinos Papadoulas.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3148

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
To draw a perspective, i am coming from, i want to use a library, that is built with long procedures, having 5-10 parametes, of those i usually really only need to set one or two.

So i naturally decided to make a wrapper with fluent-style, chained API, so i can only set those params that i actually need customized. But Fluent API requires automatic lifetime management, which in Delphi means using ref-counted interfaces.

And that comes with a problem - constructor return an object, not an interface.
And even i make all the methods on object go protected and on;y exposed via interface, i have doubts that all the compilers would successfully implicitly typecast it. Most probably they would not. And doing manual typecast damages clean and simple ease of use for Fluent-style API.

Maybe you know about Jedi CodeLib library. It supports FPC 2.4 and recently unofficial patchset for 2.6 was released.
It has a TJclStringList/IJclStringList extenstion of a stock class.

Recently on stackoverflow there came yet another question that in effect was reduced "how to read all the lines from text file and split them on some delimeter.

My usual answer for this goes along the lines
var s: string; isl1, isl2: IJclStringList;
...
isl1 := TJclStringList.Create; isl1.LoadFromFile(...);
...
for s in isl1 do begin
   isl2.Split(s, delimeter-char).Trim.Add(['some string', 'some another string']).UpperCase. ...; 
   // refining of strings can be chained

    // iterating through refined split data in isl2
end;

This time i was in a haste and drafted like
isl1 := TJclStringList.Create.LoadFromFile(...);

Well, actually that most probably would not even compile, but if it would - it would be a memory leak, because between constructor and LoadFrom*** call - there is no interface reference yet, but a PODO reference.

This obvious error, but is so easy to make when u want to type fast and fluentized.
So that gave me another reason to try to side-step Delphi object constructor.
But then how would i call factory-method? some simple name like "a", "b", "c,", "_" - would have no sense an d look ugly and hard to remember. Some lone name would harm conciseness of fluent style and would be against OOP "object.verb" style. It would be like making method TStringList.LoadStringListFromFile - DRY principle is good even in naming :-)

Then i got a flash and devised to outsmart Delphi with a construct like this:
type
  TData = class end;  // some object we need to operate upon
  IDataHandler = interface end; //some methods to make Fluent-Style, daisy-chain API

  { TDataExporter }

  TDataExporter = class(IDataHandler, TInterfacedObject)
     protected
        Payload: TData;
        constructor CreateFor(const d: TData);
        class function    MakeNewExporter(const d: TData): IDataHandler; static;

        /// all other functions, setting this or that parameter and
        /// returning Self as IDataHandler

     public
        procedure AfterConstruction; override;

        class property Factory[Payload: TData]: IDataHandler read MakeNewExporter; default;
  end;                          


{ TDataExporter }

class function TDataExporter.MakeNewExporter(const d: TData): IDataExporter;
begin
  Result := TDataExporter.CreateFor(d);
end;

procedure TDataExporter.AfterConstruction;
begin
  if Self.Payload = nil then raise Exception.Create('You should not create via inherited TObject.Create!');
end;

constructor TDataExporter.CreateFor(const d: TData);
begin
  if d = nil then raise Exception.Create('Payload data is required!');
  Self.Payload := d;
end;                    

And you know what... dunno if it works or not, but at least it compiled in CT 3.10 Win64

Please Log in or Create an account to join the conversation.

Last edit: by Arioch.

Can FPC have default class properties in Delphi ? 11 years 2 months ago #3149

  • Arioch
  • Arioch's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 59
  • Thank you received: 1
procedure TForm1.FormCreate(Sender: TObject);
var i: IDataHandler;
begin
  i := TDataExporter[TData.Create];
end;

procedure TForm1.FormCreate(Sender: TObject);
var i: IDataHandler;  d: TData;
begin
  d := TData.Create;
  i := TDataExporter[d];
end;    
unit1.pas(52,22) Error: Illegal qualifier
in both Delphi and ObjFpc mode :-(

Please Log in or Create an account to join the conversation.

  • Page:
  • 1