🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4188

Re: Nested functions

#4188
From: Barry Margolin
Date: Thu, 05 Oct 1995 22:17
48 lines
2625 bytes
In article <xePcwkXAi0eG084yn@rcp.co.uk>,
Duncan Booth <Duncan@rcp.co.uk> wrote:
>You are missing the point that the commonest use of such pointers to
>functions would be as callbacks. For example it would be extremely
>useful if qsort took a pointer to a nested function as the comparison
>function. This means that for nested functions to be useful they need
>to have the same type as any other function with the same
>parameters/results.
>
>Personally I would like to see plain nested functions without the
>ability to access outer variables. When access to outer variables is
>needed in a function, it should be passed a pointer to the variable.
>
>If you really want the ability to get at outer variables in nested
>functions, then I think the way to do it corresponding most closely
>to the C philosophy is to make it explicit. How about a new 'type'
>called (for the sake of argument) __frame, and a reserved word
>__currentframe? Here is how you might use it:

Now it seems like *you* are missing the point about the commonest use of
pointers to nested functions.  What if a callback function needs to access
outer variables?  Unless the facility making the callback provides a way
for you to register additional (presumably opaque) data to be passed in the
callback, you're SOL.

Many existing callback systems (e.g. qsort) don't provide this.  Consider
something like this:

static int second_comparison(TYPE*, TYPE*);

void fancy_sort(TYPE *array, int nel, int (*compar)(TYPE*, TYPE*)) {
    int internal_comparison(void *item1, void *item2) {
        return compar((TYPE*)item1, (TYPE*)item2) ||
           second_comparison((TYPE*)item1, (TYPE*)item2)
    }
    qsort((void *)array, nel, sizeof(TYPE), internal_comparison);
}

Unless qsort() is enhanced, there's currently no way to get "compar"
passed through to internal_comparison().  You could set a global variable,
but that makes fancy_sort() non-reentrant (what if the function supplied as
compar calls fancy_sort() internally?).  Lexical closures make this
possible.
-- 
Barry Margolin
BBN PlaNET Corporation, Cambridge, MA
barmar@bbnplanet.com
Phone (617) 873-3126 - Fax (617) 873-6351
Technical Details
Message-ID: <451lh2$p1n@tools.bbnplanet.com>
Path: arc1-ca.pugleaf.net!tools.bbnplanet.com!not-for-mail
References: <449bs9$r8c@news0.rain.rg.net> <1995Sep28.144829.1579@ittpub> <xePcwkXAi0eG084yn@rcp.co.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!tools.bbnplanet.com!not-for-mail
From: barmar@tools.bbnplanet.com (Barry Margolin)
Newsgroups: comp.std.c
Subject: Re: Nested functions
Date: 5 Oct 1995 18:17:06 -0400
Organization: BBN Planet Corp., Cambridge, MA
Message-ID: <451lh2$p1n@tools.bbnplanet.com>
References: <449bs9$r8c@news0.rain.rg.net> <1995Sep28.144829.1579@ittpub> <xePcwkXAi0eG084yn@rcp.co.uk>
NNTP-Posting-Host: tools.bbnplanet.com