From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.107.183.193 with SMTP id h184mr2583977iof.129.1510788557691; Wed, 15 Nov 2017 15:29:17 -0800 (PST) X-Received: by 10.157.89.211 with SMTP id u19mr575250otg.14.1510788557549; Wed, 15 Nov 2017 15:29:17 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.kjsl.com!usenet.stanford.edu!m191no10296itg.0!news-out.google.com!x87ni21ita.0!nntp.google.com!m191no10294itg.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 15 Nov 2017 15:29:17 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:4526:3040:7949:7829; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:4526:3040:7949:7829 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Choose between formal generic subprogram and subprogram access? From: Robert Eachus Injection-Date: Wed, 15 Nov 2017 23:29:17 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: feeder.eternal-september.org comp.lang.ada:48923 Date: 2017-11-15T15:29:17-08:00 List-Id: On Wednesday, November 8, 2017 at 8:24:17 AM UTC-5, Victor Porton wrote: > What are the good rules how to choose between formal generic subprogram v= s=20 > subprogram access argument/discriminant/field? >=20 Generics give you a way to divide the arguments to a routine into those whi= ch are determined early, and those that vary with each call. For example, you might have a routine for "pretty" printing block comments.= It would need to know the (output) line length, how much to indent the bl= ock, and of course, the source to reformat. The line length feels right as a generic parameter. The source to reformat= should definitely be a formal parameter, and the indentation is your call.= What if the routine is expected to read from a source file, and stop at t= he next non-comment line? Now the file name looks like a generic formal, p= robably along with a position in the file. But it is interesting to notice= here that how the (Ada) implementation handles line numbering may affect = your decision. The underlying idiom here is that there are three places where a generic su= bprogram can find input data: Generic formal parameters, variables (and con= stants) in the enclosing (generic) packages, and regular formal parameters.= It helps the programmer using the generic unit or package if the values n= eeded for a call are arranged such that values in the package have defaults= and are least likely to be changed, while the programmer needs to think ab= out the non-generic formal parameters whenever he writes a call. There is also a fourth place, in the body of the main program. The Ada/SIL = compiler I worked on at Honeywell started out as an extension of Green for = systems implementation purposes. So we put lots of effort into creating op= timal OS code. One long running discussion was whether to have a display, = or to walk the stack. Obviously accessing the current stack frame (and any= frames merged with it by the optimizer) and the main program body plus var= iables and constants in library level packages. Eventually I put code in where the stack walk would be to e-mail me the cod= e that called it.* I never got any e-mails, so the code never got written.= ;-) Along the way though, I collected statistics on how many variable ref= erences went to the main program, to library packages, to generic formals, = and to local variables. There were some fun things too, since this was Mul= tics, I could have statistics collected on all uses of the compiler. Use o= f variables in the main program dropped rapidly as programmers used the com= piler, and use/set for generic formals was close to one. Does this give you a feel for "the Ada way" to write code? The main progra= m is short, often shorter than its context clauses. Code in library and g= eneric packages has generic parameters that are not used much, the rest is = variables in packages or in the local subroutine that is doing the work. T= here are lots of "wrapper" routines with only a few lines that either set o= r return the value of a variable not visible to the user of the package, an= d a few large tasks or procedures that do all the work. * This sort of thing was common in compilers--and other system software--on= Multics. I, or any maintainer, could edit the compiler code, recompile it= , and continue the compiler from the point where it had paused to send the = e-mail, I had 90 seconds from the e-mail being sent to tell the compiler to= stay paused--oh and send the user involved a message saying "Please wait, = the compiler is being upgraded." Otherwise the message was: "Code generati= on error, unimplemented feature xx-xxxx."