From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Array Of Constant Strings?
Date: Fri, 14 Feb 2003 19:41:36 GMT
Date: 2003-02-14T19:41:36+00:00 [thread overview]
Message-ID: <3E4D46B9.6060805@acm.org> (raw)
In-Reply-To: 19guh-2f4.ln1@beastie.ix.netcom.com
Dennis Lee Bieber wrote:
> Ah, but perusal of the reference manual appendix A indicates that
> bounded string is a private type, which means no direct assignment to
> it.
This is not right. Assignment is defined for private types; assignment
is not defined for limited types.
Maybe that's not what was meant, but it's what it looks like to me.
>>I can throw in explicit conversion functions:
>>
>> function To_Name_String(Source: String;
>> Drop: Truncation := Right)
>> return Name_String renames
>> Name_Package.To_Bounded_String;
>>
>>and then make the Name_List this way:
>>
>> Name_List : constant array(1..26) of Name_String :=
>> (To_Name_String("Anthony"),
>> To_Name_String("Barbara"),
>> To_Name_String("Claudia"),
>> ... and so on.
Name_Package.To_Bounded_String returns Name_Package.Bounded_String, not
Name_String, which is a different type. This renaming is not accepted by
GNAT 3.15p (assuming appropriate visibility of Truncation and Right).
In any event, the derived type Name_String inherits the function
To_Bounded_String which is directly visible, so no renaming is necessary.
Some people like to use a unary operator such as "+" for common
conversions. There was a proposal for the Ada 95 revision to allow a
special unary operator with no predefined meaning for this purpose. It
wasn't accepted, and that turns out to be good, since the character for
the operator symbol that would have been used has now been changed to
the Euro symbol. Personally I think "\" would be a good operator for
this simply to give C people fits :)
So, if you rename To_Bounded_String to "+", you could write
Name_List : constant array (1 .. 26) of Name_String :=
(+"Anthony",
+"Barbara",
+"Claudia",
... and so on.
I don't like this, though. One of Ada's explicit design goals is to
emphasize ease of reading over ease of writing, and "To_Bounded_String"
is clearer than "+" to the reader. This is the opposite of C's
philosophy, in which ease of writing is everything and ease of reading
is unimportant.
Ada.Strings.Bounded.Generic_Bounded_Length is intended to be mentioned
in a use clause; it's written in such a way that things are still easy
to understand in that case. So I would recommend:
package Package_Name_Is_An_Elephant is new Generic_Bounded_Length (7);
use Package_Name_Is_An_Elephant;
Name_List : constant array (1 .. 26) of Bounded_String :=
(To_Bounded_String ("Anthony"), ...);
If all the strings are always the same length, then using fixed length
strings is even simpler. However, I would tend to be suspicious of such
a requirement; things like that are almost always changed later.
> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
> procedure aoc is
> type Name_String is new String(1..7);
>
> Name_List : constant array(1..5) of Name_String :=
> ("Anthony",
> "Barbara",
> "Claudia",
> "Deborah",
> "Elliott" );
> begin
> Null;
> end aoc;
Why is Ada.Strings.Fixed mentioned here? It's not used anywhere.
--
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
next prev parent reply other threads:[~2003-02-14 19:41 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-14 2:58 Array Of Constant Strings? Dr Nancy's Sweetie
2003-02-14 4:28 ` tmoran
2003-02-14 7:28 ` Dale Stanbrough
2003-02-14 7:47 ` tmoran
2003-02-14 10:06 ` Dale Stanbrough
[not found] ` <19guh-2f4.ln1@beastie.ix.netcom.com>
2003-02-14 19:41 ` Jeffrey Carter [this message]
2003-02-14 20:38 ` tmoran
[not found] ` <e0q0i-n14.ln1@beastie.ix.netcom.com>
2003-02-15 22:28 ` Jeffrey Carter
2003-02-22 19:31 ` Robert A Duff
2003-04-14 18:43 ` Dr Nancy's Sweetie
2003-04-15 11:36 ` Georg Bauhaus
2003-04-15 13:40 ` Dr Nancy's Sweetie
2003-04-15 15:13 ` Georg Bauhaus
2003-04-16 2:17 ` Matthew Heaney
2003-04-16 16:35 ` Chad R. Meiners
2003-04-16 21:03 ` Georg Bauhaus
2003-04-17 17:53 ` Robert A Duff
2003-04-17 20:34 ` Georg Bauhaus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox