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-Thread: 103376,9ce095aba33fe8d0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Negative float problem Date: 27 Oct 2005 10:48:24 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1130351574.313991.229420@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1130424504 28516 192.74.137.71 (27 Oct 2005 14:48:24 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 27 Oct 2005 14:48:24 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:5992 Date: 2005-10-27T10:48:24-04:00 List-Id: "Robert I. Eachus" writes: > Robert A Duff wrote: > > > But I suggest using the first solution. Really, the "use type" > > semantics ought to be turned on by default -- you shouldn't have to say > > anything special to use the "-" operator. After all, you don't have > > to say anything special to use "1.0" or ":=" on this type! > > There was an Ada83 AI on this, usually referred to as the "for I in > 1..-10" problem. (Yes the loop would be executed zero times, but the > problem is that an Ada 83 compiler was required to reject the for > statement.) A more realistic example is "for I in -10 .. 10". ;-) But that's a completely separate issue from what the OP asked about. The problem here is that Ada makes the type default to Integer. That's a kludge. I would simply require the user to write "for I in Integer range -10..10", if they want Integer. But you almost never want Integer -- you should be declaring a new integer type to suit your purpose. We've got a default that makes a poor coding practise easier! And you almost never want -10..10 -- 'Range is far more common. >...Tucker came up with some preference rules that handle some > of the cases, but the case mentioned here is one of the more refractory > ones. It could be solved by having a negation symbol that could be part > of a numeric literal. Perhaps. Would "- 10" and "-10" then have different semantics?! ^ | That's a blank. >...But as long as users are allowed to overload > operator symbols, the visibility rules will have some edge cases like > this one. > > For example > > package file1 is > > type My_Float is digits 6; > function "-"(L: My_Float) return My_Float; > end file1; > > package body file1 is > function "-"(L: My_Float) return My_Float is > begin return L-1.0; end "-"; > end file1; > > with file1; use file1; > with Ada.Text_IO; use Ada.Text_IO; > procedure file2 is > X : file1.My_Float := -1.0; > function "-"(L: My_Float) return My_Float is > begin return L+1.0; end "-"; > Y : file1.My_Float := -1.0; > begin > if X /= Y then Put_Line("Correct"); end if; > end file2; The above example is a separate issue. This anomaly would occur in exactly the same way, if you erase the "use file1", and change the language as I suggested (i.e. "use type" happens automatically). And it would occur in exactly the same way if the "-" functions were called Mumble instead of "-". Anyway, the above example is extremely confusing and error prone. If I ran the circus, both calls to "-" would be ambiguous, and therefore illegal. And the same would apply if they were called Mumble. And besides, the OP had no desire to overload any operators! It's always annoying when somebody wants to do some simple thing, and the answer from language lawyers is, "Well, if you added an upside-down generic discriminated task aggregate to the program, it would cause so-and-so anomaly." > Now in one sense this is a pretty silly example. But it is the way the > visibility and overloading rules work in Ada for good reasons that have > nothing to do with this example. ;-) Obviously it is better to have one > set of visibility rules for all subprograms than for programmers to try > to remember which set of visibility rules apply in this case. I agree that it would complicate the language to have different visibility rules for operators than for other things. But it would make the language more friendly. And the added complexity would be _less_ than the complexity we caused when we added "use type" to the language! >...(Yes, > there is a different set of rules for basic operations like numeric > literals and assignment. But since these are not overloadable, this > doesn't cause a problem.) > > Finally, there is actually a third way to make the negation operator > from file1 visible. But note that this is direct visibility not use > visible: > function "-"(L: My_Float) return My_Float renames file1."-"; That one is error prone, too. The problem here is that you don't want to rename anything, so using "renames" is overkill. The thing is still called "-" -- you don't want to rename it, you want to import it with the same name it always had. Ada has no such feature. The above sort of renaming was common for operators in Ada 83, because there was no "use type". I remember a bug that looked something like this: function "and"(...) return ... renames "and"; function "or"(...) return ... renames "or"; function "xor"(...) return ... renames "and"; I was so annoyed by this bug that I still remember it many years later! Here's another annoying example: while X /= null loop... Null is visible everywhere, but I have to type in extra mumbo jumbo to see "/=". - Bob