1) Not the only functions returning delegates, but the functions using
delegates which changes top functions local variables should have local
variables at the heap. For example:

delegate int type (int a);
void func ()
{
  int c = 2;
  make_something (x=>{return c++ + x;});
}

I mean, if lambda-expression uses function which takes local variable by
the reference, the top function should allocate their local variable at
the heap.

2) In this way any delegate translated not to common function pointer,
but to the pair of pointers: pointer to the structure holding local
variables and pointer to the function. This pair should be used through
smart pointers with reference counting. Last example then will look
like:

//----------------------------------------------------------------
typedef struct
{
  int c;
} local_vars;

typedef int (*func)(void*, int) make_something_delegate_func;

typedef struct
{
  void* arg;
  make_something_delegate_func func;
} make_something_delegate;
//----------------------------------------------------------------

void make_something ( make_something_delegate* arg);

// The lambda expression
int function (void* arg, int x)
{
  return (local_vars*)arg->c++ + x;
}

void func ()
{
  local_vars *var;
  var = (local_vars*) malloc (sizeof(local_vars));
  var->c = 2;
  make_something_delegate* deleg = smart_ptr (new
make_something_delegate); // I have never use GTK smart pointers, and 
                          // then make it looks like boost::shared_ptr 
  deleg->arg  = (void*) var;
  deleg->func = function;
  return deleg;
}
  

On Mon, 2007-12-17 at 00:31 +0300, Denis Cheremisov wrote:
> I have asked about them later, but didn't get any reply. I about if the
> function returning delegate uses not its local variables but heap. In
> this case delegates should have an addition first argument, pointing to
> the structure in the heap. For example:
> 
> delegate int type (int a);
> type func ()
> {
>   int c = 2;
>   return x=>{return c++ + x;};
> }
> 
> translated into the code like:
> ---------------------------------------------------------------
> // Local variables of the function
> typedef struct
> {
>   int c;
> } __tmp;
> 
> // The type in C, which the delegate actually has
> typedef int (*func)(void*, int) type;
> 
> // The value in C of function, returning delegate
> // First item points to the structure with "local" variables
> // Second item points to the delegate
> typedef struct
> {
>   void *local_vars;
>   type function;
> } __del_value;
> 
> // Delegate itself
> __del_value ret_func (void *str, int x)
> {
>   return (__tmp*)str->c++ + x;
> }
> 
> type func ()
> {
>   __tmp *local_vars = (__tmp*)malloc (sizeof(__tmp));
>   __tmp->c = 2;
>   __del_value result;
>   result.local_vars = (void*)local_vars;
>   result.function = ret_func;
>   return result;
> }
> ---------------------------------------------------------------
> Of course, this "closures" should be avoided if we wish to use the code
> in another language, but in Vala itself it would be a nice feature.
> 
> _______________________________________________
> Vala-list mailing list
> [email protected]
> http://mail.gnome.org/mailman/listinfo/vala-list

_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to