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,dc1fff2721602dfa X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.180.98.102 with SMTP id eh6mr73052wib.7.1359553978528; Wed, 30 Jan 2013 05:52:58 -0800 (PST) Path: bp2ni4855wib.1!nntp.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Ada and string literals Date: Wed, 30 Jan 2013 15:52:58 +0200 Organization: Tidorum Ltd Message-ID: References: <4978d638-a04b-4561-85e9-cf6620265af2@googlegroups.com> <86boc63n5d.fsf@gaheris.avalon.lan> Mime-Version: 1.0 X-Trace: individual.net dUXYxbFqwiPH31L2DN/SpAdbAQmZ7bgzhGC4lDad88+31TkXzB207KeeLPw2EDn2q5 Cancel-Lock: sha1:IU9/8IuoOlzfIkvlJPXNisWUPwE= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <86boc63n5d.fsf@gaheris.avalon.lan> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Date: 2013-01-30T15:52:58+02:00 List-Id: On 13-01-30 13:50 , Mart van de Wege wrote: > Niklas Holsti writes: > >> On 13-01-30 02:44 , codeallergy wrote: >>> hi comp.lang.ada, >>> >>> question from a newcomer: Why ada does not allow using a string >>> literal with access type ? >>> >>> abc : access string := "LITERAL"; -- error. >> >> Because, unlike C, Ada does not confuse arrays with pointers. >> >> This is the closest Ada equivalent: >> >> Literal : aliased constant String := "LITERAL"; >> abc : access constant String := Literal'Access; > > More importantly, I cannot see why you would want an access to a string > literal. > > Just use the string literal as a normal parameter, and let the compiler > worry about how to handle that. I don't know why the OP wants an access, but I can image a situation where one needs, for example, to call a subprogram with several string parameters, which should have different values depending on different logical conditions, in various combinations. It is then easier to represent each parameter by local variable that can be assigned different values in different if-then-else statements, and then use the local variables as actual parameters in the call. For example, assume you must call a procedure Foo (A, B: in String), and A can be either "James" or "John" depending on condition Alpha, while B can be either "Jill" or "Jennie" depending on condition Beta. You would like to write something like this: -- The following does NOT work: .. is A, B : String; begin if Alpha then A := "James"; else A := "John" ; end if; if Beta then B := "Jill"; else B := "Jennie"; end if; Foo (A, B); .. That doesn't work, because the declaration of A and B must specify a length for the string, and then you cannot assign literal strings of different length to A and B. If you use string literals directly as parameters, you need different calls for all combinations: if Alpha then if Beta then Foo ("James", "Jill"); else Foo ("James", "Jennie"); and so on, for a combinatorial number of cases. Of course you can use Ada.Strings.Unbounded or Ada.Strings.Bounded, which let you assign strings of different lengths to the same variable, but accesses to strings are another solution. In the above simple example one could also use Ada 2012 conditional expressions: Foo (A => (if Alpha then "James" else "John" ), B => (if Beta then "Jill" else "Jennie")); but in more complex cases that no longer works, or not as well as assigning the actual parameter values to local variables before the call. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .