[fpc-devel] Optimizing unused return values of inline functions

Martin Grajcar maaartinus at gmail.com
Wed Sep 6 02:50:15 CEST 2017


On Tue, Aug 22, 2017 at 1:15 PM, Michael Van Canneyt <michael at freepascal.org
> wrote:

>
>
> On Tue, 22 Aug 2017, Thaddy de Koning wrote:
>
> On 21.08.2017 13:22, Michael Van Canneyt wrote:
>>>
>>>>
>>>>
>>>> On Mon, 21 Aug 2017, Benito van der Zander wrote:
>>>>
>>>> Hi,
>>>>>
>>>>> This pattern is not inherently efficient. Why should it be ?
>>>>>>
>>>>>
>>>>>
>>>>> It is not efficient, because of the pointless instruction!
>>>>>
>>>>
>>>> I am not speaking of the current FPC implementation. It may well be that
>>>> the
>>>> code is not most optimal.
>>>>
>>>> I am asking, why do you think *this pattern* (of always returning self)
>>>> should be inherently more efficient ?
>>>>
>>>
>>> The pattern definitely has its uses. E.g. in the user space of our
>>> operating system at work we have a StdOutPrinter class that is used like
>>> this:
>>>
>>> === code begin ===
>>>
>>> StdIO::stdOutPrinter()->out("Helllo World ")->out(42)->out("
>>> ")->hex()->out(42)->line();
>>>
>>
> Call me old-fashioned, but I much prefer
>
>   With StdIO::stdOutPrinter() do
>     begin
>     out("Helllo World ");
>     out(42);
>     out("");
>     hex();
>     out(42);
>     line();
>     end;
>

This works well, but you can't tell the meaning of any single line, unless
you're sure that all the methods are indeed defined in stdOutPrinter.
Imagine

With StdIO::stdOutPrinter() do
    begin
    out("Helllo World ");
    out(size());
    ...
    end;

with size() belonging to the enclosing class. Someone adds size()
to stdOutPrinter and you're toasted.

I've even seen a nested WITH misuse like

with src do begin
  with dst do begin
     name := firstName + ' ' + lastName;
     amount := ownAmount;
     ...
   end;
end;

which is a terrible and error-prone misuse.


> I see no point or gain in the "fluent" code.


- It's as compact as WITH, but it's explicit.
- It can be written in a single line without violating convention.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20170906/1a0131f0/attachment.html>


More information about the fpc-devel mailing list