[Vala] Closures in vala



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.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]