Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!bs From: bs@alice.UucP (Bjarne Stroustrup) Newsgroups: net.lang.c Subject: lambda defs in C (and C++ inlines) Message-ID: <4952@alice.uUCp> Date: Sat, 8-Feb-86 16:20:57 EST Article-I.D.: alice.4952 Posted: Sat Feb 8 16:20:57 1986 Date-Received: Tue, 11-Feb-86 02:58:19 EST Organization: Bell Labs, Murray Hill Lines: 65 > From allegra!mit-eddie!think!harvard!seismo!hao!nbires!isis!dmiruke Wed Dec 31 19:00:00 1969 > From: dmiruke@isis.UUCP (Dataram Miruke) > Subject: Re: lambda defs in C > Organization: University of Denver Math and Computer Science > >> I often find myself creating highly localized register variables in an >> attempt to synthesize common sub expression optimization: >> >> { >> register char _q = *((x.y)->z); >> >> if (_q == 'a' || _q == 'b' || _q == 'c') { ... >> >> I also want to return values from arbitrary expressions like a for loop. >> Lisp has something called a lambda definition which is something like >> a local temporary function definition. Sort of like: >> >> if ( lambda int f(register char _q = *((x.y)->z)) { >> return(_q == 'a' || _q == 'b' || _q == 'c'); >> } /* end of lambda def */ ) { ... >> >> or >> >> x = lambda int f() { >> for (...) { >> ... >> return(some expression); >> ... >> } /* for */ >> } /* lambda */ >> >> So, academically, if we were to add a simlar feature to a new dialect of C, >> or to construct a pre-preprocessor for ansii C (a la C++), what problems >> do you see? >> >> K. Richard Magill > Don't the inline functions in C++ provide a similar feature? > - Datta Miruke Yes, the following is legal C++ inline int f() { for (...) { ... return(some expression); ... } } However, the current implementation cannot handle loops or switches in an inline, so you get a ``sorry, not implemented'' message. Sorry, we will have to wait a bit longer. The non-looping example can be handled quite nicely using an inline. Inline functions differ from lambdas in that they must be defined (outside any function) before they can be used. - Bjarne Stroustrup (AT&T Bell Labs, Murray Hill). PS Calling the current C++ implementation a pre-processor for C is as accurate as calling C a pre-processor for the assembler. The C++ front-end does a complete typecheck and performs non-trivial code transformations (like inline expansion). It relies on the underlying C compiler for the part of the code generation process (only). ANY message from the C compiler is considered a C++ compiler bug.