lazarus at mfriebe.de
Sun Dec 26 22:57:01 CET 2021
On 26/12/2021 02:16, Blaise--- via fpc-devel wrote:
> I propose that the support for
> https://en.wikipedia.org/wiki/Function_object be added to the FPC.
> aC := C.Create;
> writeln( aC(33) );
I might be barging in a bit late. But (within the given set that we have
/ not within the set of any other language):
Is this a new feature? Or a shortcut? Or maybe more precise, how much of
it is new.
To start with, the above examples are probably intended to show the
syntax. But not indented to show any useful application?
Lets go to the example (I think from the wiki).
Passing the functor, instead of a function ref:
MyList.Sort( C.Create('sort_field') );
This is (mostly) a shortcut for
MyList.Sort( @C.Create('sort_field').Invoke );
2 things are different in the above.
1) The instance must be ref-counted, and free itself => or you get a mem
That part could be regarded as a new feature. Well, actually, my
understanding is that feature comes under the name of "smart pointer" ?
(but I may be wrong on that one)
2) Acts as a shortcut.
- removes the "@"
which in mode objfpc, really belongs to passing on a reference to
something that can be called.
Yes, if it were a class (and therefore a ref by itself), it would not
be needed, but it is a functor, something that can be called.
- removes the name of the called method.
Actually a pity. Typing out the name, means the ability to chose a
diff method, for diff purposes.
So based on this, and
- if "smart pointer" does bring the required ref-count,
- if we do not want to duplicate "smart pointer"
then what in "functor" is a new feature (rather than shortcut), compared
to the feature already on offer (or under development)?
One more question.
If I am right in the assumption, that a functor is meant to be
ref-counted, then by which criteria is the distinction between "functor"
and "class" made.
In the example a "functor" is declared as a "class".
But a class is not ref-counted. And changing that for every class is not
an option, since existing code would not work with this.
So then, how would the compiler know, that a "class" is a "functor" and
- Because it has an "invoke" function (original suggestion)? (that
clashes with existing code)
- Because it has a function that has a "default" modifier?
That would be possible, but I think it is not explicit enough.
A simple "default" somewhere on one function, hidden in a list of who
knows how many functions?
So then, the ref-count would need to go as a modifier to the "class" itself.
Not sure how smart pointers (when used for classes) are to be declared....
If I missed anything in the proposal that differs majorly from my
understanding, then sorry about the noise.
More information about the fpc-devel