Path: utzoo!utgpu!attcan!uunet!husc6!cmcl2!nrl-cmf!ames!ncar!oddjob!uxc!uxc.cso.uiuc.edu!a.cs.uiuc.edu!p.cs.uiuc.edu!gillies From: gillies@p.cs.uiuc.edu Newsgroups: comp.lang.modula2 Subject: Re: C v.s. Modula II Message-ID: <79500010@p.cs.uiuc.edu> Date: 15 Aug 88 02:45:00 GMT References: <79500004@p.cs.uiuc.edu> Lines: 74 Nf-ID: #R:p.cs.uiuc.edu:79500004:p.cs.uiuc.edu:79500010:000:2489 Nf-From: p.cs.uiuc.edu!gillies Aug 14 21:45:00 1988 I will elaborate, since I think parts of my posting were misunderstood: >Adding an else-clause can require you to restructure the {}'s. Here is an extreme example, and many others exist: if (a) if (b) if (c) do_something_useful(); /* Add an else-clause for the second -if- produces */ if (a) { if (b) { if (c) do_something_useful(); } else c_sucks_the_big_wazoo_sometimes(); } Look at all the braces which had to be added to make the else clause bind correctly! >Often, for(;;) loops are written with *no body*, and adding additional >commands can require major restructuring of the loop. /* Here is a way to copy a string */ for(p=charbuf, q=copybuff; *p++=*q++;); /* What if I wanted to debug this, and print characters as they were copied? */ for(p=charbuf, q=copybuff; *q;) { putchar(*p++=*q++); /**/ } Notice how I had to restructure the looping condition to make this change. Things can be much worse with a more complicated loop. >6. C has no provision for callback procedures (you need nested >procedures and the right semantics for the 'display'). So data >abstraction is hindered in C. Let us say I have a data-abstract code package that exports a procedure that traverses a dictionary data structure. The format of the data structure is proprietary. Callback requires ONE level of procedural nesting for each nested loop. How can I express something like this in C? dict: Dictionary.Handle; -- global variable PrintDictionary: PROCEDURE = { wh: Window.Handle := Somewhere.GetAWindowHandle[]; -- used in callback -- A CallbackProc is a PROC[a:LONG STRING] RETURNS[BOOLEAN]; ShowName: Dictionary.CallbackProc = { Put.String[wh, a]; Put.CR[wh]; RETURN[TRUE] } Dictionary.Enumerate[dict, ShowName]; } I call the Dictionary.Enumerate[] procedure, and it repeatedly makes call to the ShowName procedure. FURTHERMORE, from within the ShowName procedure, I can access & change the variables global to PrintDictionary (wh, the window handle). This is crucial if you want to get something done, in a type-safe manner. I don't think C++'s pointer parameters cut the mustard..... Callback procedures are a nice way of supporting abstract enumeration when the language lacks something like CLU's explicit enumerators. Don Gillies, Dept. of Computer Science, University of Illinois 1304 W. Springfield, Urbana, Ill 61801 ARPA: gillies@cs.uiuc.edu UUCP: {uunet,ihnp4,harvard}!uiucdcs!gillies