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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,UTF8 X-Google-Thread: 103376,5cb36983754f64da X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-07 19:16:50 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newspeer.monmouth.com!newspeer1.nwr.nac.net!news.worldonline.be!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: No call for Ada (was Re: Announcing new scripting/prototyping language) Date: 08 Feb 2004 04:15:55 +0100 Organization: Worldonline Belgium Sender: lbrenta@lbrenta Message-ID: References: <20040206174017.7E84F4C4114@lovelace.ada-france.org> <54759e7e.0402071124.322ea376@posting.google.com> NNTP-Posting-Host: ppp-62-235-80-7.tiscali.be Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.worldonline.be 1076210200 13670 62.235.80.7 (8 Feb 2004 03:16:40 GMT) X-Complaints-To: abuse@worldonline.be NNTP-Posting-Date: Sun, 8 Feb 2004 03:16:40 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Xref: archiver1.google.com comp.lang.ada:5330 Date: 2004-02-08T04:15:55+01:00 List-Id: Dear "MSG", I do not normally respond to people who won't tell me their name. Nevertheless, I found your questions interesting, so here goes. msg1825@yahoo.com (MSG) writes: > Ludovic Brenta wrote... > > [...] > > > The "zen master" languages are Pascal, Modula, > > Oberon, and, master of masters, Ada. The beauty of these languages is > > that, once you are Enlightened, you can apply your wisdom to other > > languages as well -- but often would prefer not to. > > > Can you do the following in Ada: > > 1. Write *one* bubble-sort function that will work on different > types given an appropriate comparison function Yes you can, using generics. This is actually part of the library that comes with the GNAT compiler. See the spec for the package at http://savannah.gnu.org/cgi-bin/viewcvs/gcc/gcc/gcc/ada/g-busorg.ads?rev=HEAD&content-type=text/vnd.viewcvs-markup And the body at http://savannah.gnu.org/cgi-bin/viewcvs/gcc/gcc/gcc/ada/g-busorg.adb?rev=HEAD&content-type=text/vnd.viewcvs-markup. > 2. If B is a subtype of A, can you pass it to any function that > takes A as an argument? (covariance) Yes, as illustrated below. Covariance, as far as I understand, is the capability to override the function using B as a type for both the parameter and the return type. Ada has this capability. Note that Ada has no hidden "this" parameter; everything is explicit. with Ada.Text_IO; use Ada.Text_IO; procedure Covariance is package P is type A is tagged null record; function Operation (This : A) return A; procedure Some_Primitive_Operation (Parameter : in out A); type B is new A with null record; function Operation (This : B) return B; -- is a covariant function: the types of the parameter and the return -- type vary together end P; package body P is function Operation (This : A) return A is begin Put_Line ("Operation on A"); return This; end Operation; procedure Some_Primitive_Operation (Parameter : in out A) is begin Put_Line ("This accepts A or any derived type thereof"); end Some_Primitive_Operation; function Operation (This : B) return B is begin Put_Line ("Operation on B"); return This; end Operation; end P; use P; My_First_A, My_Second_A : A; My_First_B, My_Second_B : B; begin My_Second_A := Operation (This => My_First_A); My_Second_B := Operation (This => My_First_B); Some_Primitive_Operation (Parameter => My_Second_B); end Covariance; > 3. If B is a subtype of A, and FA and FB are functions accepting A > and B as arguments, can you use FA wherever FB could be used? > (contravariance) I'm not sure I understand what you mean. Does the following code answer your question? If it doesn't, please post a compilable code snippet in your preferred language. with Ada.Text_IO; use Ada.Text_IO; procedure Contravariance is package P is type A is tagged null record; function Func (This : in A) return A'Class; -- this would be FA type B is new A with null record; function Func (This : in B) return A'Class; -- this would be FB end P; package body P is function Func (This : in A) return A'Class is begin Put_Line ("Func on A"); return This; end Func; function Func (This : in B) return A'Class is begin Put_Line ("Func on B"); return This; end Func; end P; use P; My_First_A, My_Second_A : A; My_First_B, My_Second_B : B; begin My_First_A := A (Func (My_First_A)); -- explicit conversion to type A My_Second_A := A (Func (My_First_B)); -- explicit conversion to type A end Contravariance; > 4. If B is a subtype of A, is list/array/vector/set/etc. of Bs a > subtype of list/array/vector/set/etc of As? (covariance) Not automatically. You would define new containers explicitly for A and B, using generics; see for example the Booch components[1] or the Charles library[2], which is modelled after the C++ STL. If you want polymorphic containers, you store pointers in them. [1] http://www.pogner.demon.co.uk/components/bc/case-study.html [2] http://home.earthlink.net/~matthewjheaney/charles/ > Unless you can show us how to do this in a way that will keep Ada a > "safe" (third category) language you say it is, I will not believe > that it's a "master of of the masters", I'm afraid. I cannot show you something you refuse to see (you say you won't learn Ada). But if you are curious and intellectually honest, you will see that Ada is indeed safer almost all other languages. If Ada is not safe enough for you (even though it is for Boeing and Airbus and the French TGV), then look into SPARK[3]. SPARK is a subset of Ada where dynamic memory allocation and several other potentially dangerous constructs are forbidden. When I say "subset", I mean that any Ada compiler can compile any SPARK program. It also adds design by contract à la Eiffel, using a tool called the "Examiner" that complements the compiler. [3] http://www.sparkada.com > If you answer "yes" to any of the questions, post *compilable* > snippets: we don't want to learn Ada just to verify your claims, > we simply won't believe you. You would be well advised to learn Ada. If you don't do it just to verify my claims, do it to better your understanding of safety, maintainability and large-scale programming in general. As I said, if you accept to be given lessons, you can then reapply this insight to your language of choice. > BTW, the esteemed Mr. E. Robert Tisdale (ER for short) isn't > letting on about why Ada isn't used much at NASA any more. > Perhaps *you* have an explanation? My earlier post was an attempt at an explanation. To rephrase it shortly, few people are willing to be slapped on the hand by the compiler every time they make a mistake; they'd rather ship buggy software to customers ("True Programmers"), or use a language that makes critical decisions unbeknownst to them ("Programming made easy"). This explanation, of course, is just a theory of mine and is a caricature more than an accurate description of reality. -- Ludovic Brenta.