Unnamed functions, functors or, more formally, function literals

Kris Warkentin kewarken@qnx.com
Fri Mar 15 09:55:00 GMT 2002


----- Original Message -----
From: "Robert Dewar" <dewar@gnat.com>
To: <kewarken@qnx.com>; <pkoning@equallogic.com>; <sd70030@lanet.lv>
Cc: <gcc@gcc.gnu.org>
Sent: Friday, March 15, 2002 11:09 AM
Subject: Re: Unnamed functions, functors or, more formally, function
literals


> <<I'm not saying that this should be added to C since, well, then it
wouldn't
> be C now would it?  But I wouldn't write them off as less than useful.
Try
> using it for GUI programming some time.  It's wonderful to pass a function
> to be called when such and such button is pressed without having to define
> it in the global or class namespace, especially if the action for the
button
> is something very simple.
> >>
>
> Puzzlement ... in GNU C, you can perfectly well pass the address of a
> local function. Now if you are saying you want full closures so that there

No, I was thinking along the lines of java's anonymous inner classes or
Python's lambda functions or even Perl's anonymous functions:

Like in perl where you go $myfunc = sub BLOCK and now you have a reference
to an anonymous function that you can pass around to do things with.  It's
nice to avoid having a full declaration of a function like

int
mystruct_do_something(struct mystruct *p)
{
    /* blah */
}

so you can go:

mystruct_apply_func(struct mystruct *p, mystruct_do_something);

when you really just want:

mystruct_apply_func(struct mystruct *p, imaginary_lambda_op{ /*blah*/ });

cheers,

Kris
> are no out-of-scope problems, that's OK, but actually not many languages
> *do* have full closures, and they are very hard to implement in the
absence
> of garbage collection (in fact I don't see anyway of implementing them
with
> no GC).
>
> Certainly the suggestion of adding full closures to C is way out of scope.
>
>



More information about the Gcc mailing list