Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!ames!lll-winken!uunet!mcvax!kth!sunic!dkuug!freja!njk From: njk@freja.diku.dk (Niels J|rgen Kruse) Newsgroups: comp.lang.c Subject: Re: C macros (was comma operator: keep away?) Message-ID: <4626@freja.diku.dk> Date: 27 Apr 89 23:54:08 GMT References: <19913@iuvax.cs.indiana.edu> <10092@smoke.BRL.MIL> <1317@ns.network.com> <17109@mimsy.UUCP> Organization: DIKU, U of Copenhagen, DK Lines: 63 chris@mimsy.UUCP (Chris Torek) writes: >and/or with each other's). What is missing from C (for operations like >getchar and putchar) is not better macros, but rather inline functions. Inline functions are missing one of the main advantages of C macros over most other mechanisms for abstraction : polymorphism. What i would like to see in C is a *let expression* that would allow me to write #define swap(a,b) ([f = &(a), s = &(b)][temp = *f] *f = *s, *s = temp) #define square(x) ([temp = (x)] temp * temp) #define max(a,b) ([f = (a), s = (b)] f > s ? f : s) and have j = max (square (rand()),*++i); be robust to sideeffects and have both char *p,*q; swap (p,q); and double x,y; swap (x,y); work as intended. Syntax : let-expression ::= [ let-list ] result-expression let-list ::= let-item | let-list , let-item let-item ::= identifier = expression Precedence of let-expression below comma-expression (Result-expression is any kind of expression, i just need to distinguish it from those in the let-list) Semantics : Scope of the identifiers introduced in the let-list is the result-expression. Their types and values are those of the expression in the introducing let-item, evaluated prior to evaluating the result-expression, ie. the ] is a sequence point. Evaluation order among let-items is undefined. The identifiers introduced have no location, ie. & (address operator) can not be applied to them and they can not be assigned to. The value and type of the let-expression is that of the result-expression. This language construct is borrowed from letlisp. A direct borrow would have let-expression ::= let let-list in result-expression but keywords in expressions would be unlike C. A [ without an expression in front of it is a syntax error in current C, so the overloading is feasible. -- Niels J|rgen Kruse Email njk@diku.dk Mail Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark