[fpc-pascal] Managed properties idea

Marcos Douglas B. Santos md at delfire.net
Sat Oct 7 17:44:27 CEST 2017


On Fri, Oct 6, 2017 at 4:49 PM, Michalis Kamburelis
<michalis.kambi at gmail.com> wrote:
> 2017-10-06 20:52 GMT+02:00 Marcos Douglas B. Santos <md at delfire.net>:
> [...]
>>> In this case, in which you indeed want two of these features simultaneously,
>>> I advise COM interfaces myself :) That's why they are documented after all.
>>
>> I can use it only to use reference counting.
>
> You can, but it's a little uncomfortable.
>
> If you use interfaces only for reference counting (not to share the
> API between some unrelated classes), then you define a single
> interface for each single class, repeating the same API -- methods,
> properties. It's possible, but it's just a lot of work. I have a lot
> of classes in my engine, and FPC RTL, FCL, Lazarus LCL define many
> more classes --- wrapping them all in an equivalent interface, only to
> get reference-counting for them, would imply a lot of work and
> constant maintenance.

I use interfaces because, IMO, this is the right way to write
object-oriented code.
First you start with the interface that will be the contract. Then,
you create one or more classes "to give life" an object which
represents a real entity. Finally, every method argument or class
attribute, should be an interface-type variable — most cases.
The repetition here is due to the language design, but I'm OK with it.
Sometimes you may have 1:1 (1 interface, 1 class) but you can have
1:N.
You (or just me) don't need to create an interface with the same
methods/properties to use FPC RTL, FCL, Lazarus LCL just to wrap them
to use reference-counting. This might be wrong at most cases. That is
code duplication.
We already have this infrastructure in FPC RTL, FCL, Lazarus LCL — not
perfect, but... — and we don't need to recreate them in our business,
we just use them. So, for example, if I need to open a file, I will
have an interface that represents a file in my business and then I
will use a TFileStream class encapsulated in my own representation
(class) of my interfaced.

Most cases we don't need the dozens methods which many classes have —
even in Java or C# do that.

> In contrast, something like C++ "shared pointer" is a feature that can
> be applied to any existing class, without the need to repeat it's API.
> See e.g. https://en.wikipedia.org/wiki/Smart_pointer . And once we
> have management operators (see Sven's pointers), we can implement
> something like this in Pascal. So you could use something like
> "TSharedPointer<TStringList>" and you get a reference-counted instance
> of TStringList.

OK, it sounds interested but would be a better design if a
"modeswitch" turn on this as default for entire unit, ie, every
instance will be reference-counted.

Best regards,
Marcos Douglas



More information about the fpc-pascal mailing list