[fpc-pascal] with in classes/records
Ryan Joseph
ryan at thealchemistguild.com
Mon Sep 3 09:15:08 CEST 2018
I’m sorry to bring this one up again but it was touched upon in regards to management operators and auto free objects. Please bear with me while I recap.
To summarize what I said last time was that I wanted a way to include “with” statement functionality in classes and records to aid in delegation patterns (in leu of multiple inheritance in Pascal). As it turns out the idea is relevant to management operators and something similar using properties was requested by the developer of management operators (Maciej).
He suggested a “default” property which would basically delegate methods/field calls to another class so to avoid having to do things like "obj._.Free” which would be shorted to “obj.Free” because the field “_” is default. As he pointed out if such a default property existed I could leverage management operators to auto manage objects instead of adding anything more to the compiler like I did in my version. That’s a really important advancement for FPC and I’m personally very motivated in seeing this manifest in the language.
Maciej’s example:
TAutoCreate<T: class> = record
_: T;
property obj: T read _; default;
class operator Initialize(var a: TAutoCreate<T>);
class operator Finalize(var a: TAutoCreate<T>);
class operator Copy(constref a: TAutoCreate<T>; var b: TAutoCreate<T>);
class operator AddRef(var a: TAutoCreate<T>);
end;
My suggestion I made some months ago is functionally identical except I wanted to add a “with” modifier to fields with no explicitly defined limit (or make it a section like public, or what ever, I don’t care really). i.e:
type
TSomeManager = record
procedure ManageThis;
end;
type
TOtherManager = record
procedure ManageThat;
end;
type
TMyRec = record
delegateA: TSomeManager; with;
delegateB: TOtherManager; with;
end;
var
c: TMyRec;
c.ManageThis; // delegateA is “with” so we can access it’s fields from TMyRec
c.ManageThat; // delegateB is “with” so we can access it’s fields from TMyRec
I’m writing this email today because I just had this exact same problem in my code and I’m desperate to make this work better in Pascal. :) Making auto free work as intended would be amazing also but we need to pass this hurdle first.
Thank you for bearing with me, so finally here are my questions:
1) Given this is critical to make management operators work smoothly what does the compiler team think about this idea to have a default property or “with" in classes/records?
2) If there is any plausible way the compiler team will allow this I’m willing to develop it myself to what every specification the team decides. I already looked into how this could be implemented and did some tests. It’s not complicated and within the range of something I could accomplish.
Please let me know what you guy think of this proposal.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list