[fpc-pascal] Syntax changes suggestions

Santiago A. svaa at ciberpiula.net
Tue Jul 17 10:19:00 CEST 2018


El 16/07/2018 a las 21:27, Marco van de Voort escribió:
> In our previous episode, Sven Barth via fpc-pascal said:
>>> function)
>>>
>>> In such cases, you don't declare it "auto". Just as you don't free a
>>> pointer in the function you declare it if you pass the instance to another
>>> code that stores it beyond the life time of the function
>>>
>> But you don't necessarily know that the function you call does that (think
>> third party code). And people *will* use this and don't think about the
>> consequences. So a system with automatic reference counting is safer and
>> that is what is planned, if at all.
> Moreover the use case, dynamically instantiated classes with very local
> scope is rare.
You must be kidding. You use local scope objects everywhere. The 
TStreams family is a clear example.

source/rtl/objpas/classes/classes.inc

//----------------------------------------
function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: 
TComponent): Boolean;

   procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
     var
       w : twriter;
     begin
       w:=twriter.create(s,4096);
       try
         w.root:=o;
         w.flookuproot:=o;
         w.writecollection(c);
       finally
         w.free;
       end;
     end;

   var
     s1,s2 : tmemorystream;
   begin
     result:=false;
     if (c1.classtype<>c2.classtype) or
       (c1.count<>c2.count) then
       exit;
     if c1.count = 0 then
       begin
       result:= true;
       exit;
       end;
     s1:=tmemorystream.create;
     try
       s2:=tmemorystream.create;
       try
         stream_collection(s1,c1,owner1);
         stream_collection(s2,c2,owner2);
         result:=(s1.size=s2.size) and 
(CompareChar(s1.memory^,s2.memory^,s1.size)=0);
       finally
         s2.free;
       end;
     finally
       s1.free;
     end;
   end;


//----------------------------------------
// auto version
//----------------------------------------

function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: 
TComponent): Boolean;

   procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
     var
       w : twriter;auto;
     begin
       w:=twriter.create(s,4096);
      w.flookuproot:=o;
       w.writecollection(c);
     end;

   var
     s1,s2 : tmemorystream; auto;
   begin
     result:=false;
     if (c1.classtype<>c2.classtype) or
       (c1.count<>c2.count) then
       exit;
     if c1.count = 0 then
       begin
       result:= true;
       exit;
       end;
     s1:=tmemorystream.create;
     s2:=tmemorystream.create;
    stream_collection(s1,c1,owner1);
     stream_collection(s2,c2,owner2);
     result:=(s1.size=s2.size) and 
(CompareChar(s1.memory^,s2.memory^,s1.size)=0);
   end;
//----------------------------------------

With "Auto", you save a lot of "try finally free" that add nothing to 
algorithm

You can argue against "auto" in the grounds of "Aesthetic symmetry ", 
"it's not explicitness pascal way", "it's not worth", "confusion mixing 
styles/paradigms" or other arguments I haven't thought. I asked 
expecting those arguments I hadn't thought about.
There may be valid arguments against, but when I read "local scope for 
classes is rare", I know I am in the grounds of a irrational 
resistance.  In such cases, a "For the sake of brevity, my vote is 
simply "no" to all your suggestions." is the best answer.

-- 
Saludos

Santiago A.




More information about the fpc-pascal mailing list