roxen.lists.pike.general

Subject Author Date
Re: thread and lambda Mirar @ Pike importmöte för m <6341[at]lyskom[dot]lysator[dot]liu[dot]se> 23-03-2009
How about

 mapping n=([]);

 void main()
 {
    mixed c=lambda(string s)
    { 
       string k,b; 
       sscanf(s,"%s=%s",k,b); 
       n[k]=b;
    };

    function c2(string s)
    { 
       string k,b; 
       sscanf(s,"%s=%s",k,b); 
       n[k]=b;
    };

    mapping m=([]);

    function d(string s,mapping mloc)
    { 
       string k,b; 
       sscanf(s,"%s=%s",k,b); 
       mloc[k]=b;
    };

    Thread.Thread(...c2,m...);
 }

You'd end up warning for everything, or missing a lot of cases where
you might do something wrong. If c and c2 shouldn't be warned for,
consider

  void foo()
  {
     mapping n=([]);
    
     void main()
     {
        mixed c=lambda(string s)
        { 
           string k,b; 
           sscanf(s,"%s=%s",k,b); 
           n[k]=b;
        };
    
        Thread.Thread(c);
     };

     main();
  }

In d, the function looks ok, but the mapping is still not local
because of how it's used. Since most datatypes are destructive, it
might very well change value under foot, to take the first example:

  class Counter { int x; };
  Counter my_counter=Counter();
  for(my_counter->x=0;my_counter->x<100;my_counter->x++){
      Thread.Thread(lambda(){
          write("%d\n",my_counter->x);
      });}
  
  Counter my_counter;
  for(my_counter->x=0;my_counter->x<100;my_counter->x++){
      Thread.Thread(lambda(Counter local_counter){
          write("%d\n",local_counter->x);
      },my_counter);}

And the other problem is that I wouldn't really know where to start
untangling stuff to be able to give useful warnings, since the warning
has to be when you use a function pointer, which is fairly anonymous.