[fpc-devel] New feature discussion: LAMBDA

Dariusz Mazur darekm at emadar.com
Tue Oct 20 16:49:28 CEST 2009


Hi
 Based on discussion about "for in" I want to suggest my proposition.
When we investigate some code, sometimes we found pattern:

Pattern 1:
/  begin
    startfun;
    try
      ...
    finally
      stopfun;
    end;
/  end;

Pattern 2:
  for i:= startfun to stopfun do begin
     ....
  end;


Pattern 3:
  startfun;
  while stopfun do begin
    ...
  end;


Common for all mentioned patterns are two connected functions, which 
controlled running of body source. First make all initializations, 
second  helping with iterations and finalizations.

My proposition is make possibility of join, those  pair to one  
sentence. All patterns can be change to one common template:

   if startfun then repeat
   until stopfun;
  
I You see we need two function with boolean result. Of course 
possibility of parameters give wide appliance.

To preserve  pascalish  style I propose something like this:

 tMyObject = class(tObject)
   function startfun:boolean;
   function stopfun:boolean;
  property lambdafun; start startfun stop stopfun;
 end;

 and invoke:
  
  var
   t : tMyObject;
  begin
   ..
   t.lambdafun do begin
      ....
   end;
  end;
 
because on declaration side is similar do properties (also two function) 
we can use the same syntax. Only keyword  start and stop  exchange  read 
and write.

To using i thought  about  WITH, but it may confusing bit, thus  I 
propose omit, and only use keyword DO.

Why lambda. Because is very similar to constructing no name procedures 
and use them as normal. We have few mechanism controlling runnig: IF, 
REPEAT, WHILE, FOR. They work well, but sometimes they are week. For 
example we don't have enough primitives do build own implementation 
FORIN method, on iterators also. But in Lisp any mechanism, any sequence 
of computing collection can be done and source is very readable
  lisp syntax: (lambda (<formal parameters>) <body>)
This is not unusual, but we can send this declaration as argument of 
procedure.
Of course syntax like this
   tlist.foreach( procedure (x : tObject);begin writeln(x) end)

is ugly (thought I sow something similar in  C++/boost)
but when we change it a bit:

  tlist.foreach(x) do  begin
    writeln(x);
  end;

its not so strange

Because my english is very poor I only show some application

EXP. mutex (and patterns as RIAA)

    tMutex = class
      property makeatomic; start lock stop unlock;
    end;
  .....

  begin
    tMutex.makeatomic do begin
      ...
    end;
  end; 

EXP: foreach


   tMyList = class
     
     function tMyList.start:boolean;
     begin
       current:=first;
       result:=current<>nil; 
     end;
     function tMyList.stop:boolean;
     begin
       current:=next(current);
        resutl:=current<>nil;
     end; 

 
When attributes are added, or exception caching this will be more flexible.


 
 

-- 
  Darek








More information about the fpc-devel mailing list