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,ASCII-7-bit X-Google-Thread: 103376,38c827f7e800d317 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-27 15:14:02 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn14feed!wn13feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail Message-ID: <3EFCC18B.4040904@attbi.com> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: conversion References: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.62.164.137 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1056752034 24.62.164.137 (Fri, 27 Jun 2003 22:13:54 GMT) NNTP-Posting-Date: Fri, 27 Jun 2003 22:13:54 GMT Organization: AT&T Broadband Date: Fri, 27 Jun 2003 22:13:54 GMT Xref: archiver1.google.com comp.lang.ada:39852 Date: 2003-06-27T22:13:54+00:00 List-Id: Alexander Kopilovitch wrote: > The chorus also tells you that in Ada you should not need those conversion > too often. Well, they are honest enough, they simply do not use Ada for > a job and in an environment where you must mix varying strings and constant > strings here and there, and at the same time you can't afford several weeks > for design for every change in specifications. > > Ada simply understimates need of strings in non-numerical world (almost like > Fortran IV, that is, before Fortran 77). I'm sure that Ada people would cry > very loudly if there were similar situation with Integer type: imagine that > there is unconstrained (for value, not for storage size) type Integer, which > objects must be constrained (by value). Then, there are types Bounded_Integer > and Unbounded_Integer. Unbounded_Integer is good for general use, except that > you always have to write explicit conversion when you assign a literal (say, > 1 or 0) to an object of Unbounded_Integer type (currently Ada Integer is like > a Pascal string in this respect). This is silly. The situation in Ada is exactly the same with integer and string types. Objects must be constrained, values need not be. If I write: X: Integer; ... X := X + 1; ... the result may not fit in an Integer and I'll get Constraint_Error. But the Constraint_Error occurs because of this value/object dichotomy, just as if I had tried to assign an 81 character string to an 80 character buffer. The Ada.Strings.Bounded and Ada.Strings.Unbounded packages provide a way to create objects that have only a maximum or no minimum and no maximum length respectively. The equivalent for integer types is a bignum package, which supports any size integer value without Constraint_Error. Now compile and run this code: with Ada.Text_IO; use Ada.Text_IO; procedure NumEx is X: Integer := 2**65 / 3**38 + 3; begin Put_Line("The value of X is" & Integer'Image(X)); end NumEx; If it doesn't compile without error and print: The value of X is 30 Send a bug report to the compiler vendor. What happened there? If you don't see why there would be a problem in most languages, go back and look more carefully. I'll wait. I suspect that 2**65 is out of range for any predefined integer type for your compiler. (If it isn't, make up your own example.) This behaviour is required by RM 8.6(29) which states a preference for operations for root integer and root real. These rules require every Ada compiler to implement a bignum package and a rational arithmetic package and use them at compile time in some cases. If you are a language lawyer, you should know how to "take advantage" of these special rules to do (numerical) things in Ada. Of course, if you need unbounded rational arithmetic objects not values, the best way is to snarf the rational arithmetic package from the compiler. It isn't as easy to use as using Integer--for exactly the same reasons that using Unbounded_String is not quite as easy as using String. Of course, as I showed above, you can use these exended arithmetic features of Ada very easily and not really know that you are doing it. To sum up, everything you can do with char[] and char* in C can be done in Ada with String. There are things you can do with String in Ada that are not possible in C. (Of course, in the sense that both languages are Turing complete, that isn't true, but you get my meaning.) There are things you can do in PL/I with char * varying, that you can do in Ada with Ada.Strings.Bounded. There are things that you can do in C++ with the class str that you can do in Ada with Ada.Strings.Unbounded. If you are bugged by writing To_String, and To_Unbounded_String, go ahead and create your own functions. THERE IS A BIG CATCH HERE. But let me show you what I mean. First, look at all the functions that are available in Ada.Strings.Unbounded. There is a lot of unexpected wealth there. For example: with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO; use Ada.Text_IO; procedure Fun is U1: Unbounded_String := 3 * 'A'; U2: Unbounded_String := 2 * "can"; S1: String := " dance"; function "&"(L: String; R: Unbounded_String) return String is begin return L & To_String(R); end "&"; begin Put_Line(" U1 is " & U1); Put_Line(" U2 is " & U2); Put_Line(" U2 & S1 is " & U2 & S1); end Fun; So what is the catch? Notice that I only created one new "&" operation, if I had added: function "&"(L: Unbounded_String; R: String) return String is begin return To_String(L) & R; end "&"; as well, then the program wouldn't have compiled. I could use a qualified expression inside the third Put_Line to determine which "&" functions got called, but best is not to have too may operators floating around. Adding one or two can really help clean up your code, adding too many leads to madness. Personally I think that the functions that return String are more useful, so I could have avoided a use clause for Ada.Strings.Unbounded But in this example I wanted to show the multiplier notation as well.