comp.lang.ada
 help / color / mirror / Atom feed
* String literals and wide_string literals - how?
@ 2007-04-20 10:07 Gerd
  2007-04-20 14:33 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Gerd @ 2007-04-20 10:07 UTC (permalink / raw)


Hi all,

if I have a string, then I can write a literal simply by writing
"abcd". But if I want write a wide_string literal, how do it?

Assume I have two procedure of the form:

procedure do_anything (s : string);

procedure do_anything (w : wide_string);

then

do_anything ("abcd");

would call the first one. What would one write to call the second
one?

Is there something like the L"xxx" in C?

Gerd




^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 10:07 String literals and wide_string literals - how? Gerd
@ 2007-04-20 14:33 ` Adam Beneschan
  2007-04-20 14:55 ` Robert A Duff
  2007-04-20 14:58 ` Georg Bauhaus
  2 siblings, 0 replies; 24+ messages in thread
From: Adam Beneschan @ 2007-04-20 14:33 UTC (permalink / raw)


On Apr 20, 3:07 am, Gerd <Gerd...@t-online.de> wrote:
> Hi all,
>
> if I have a string, then I can write a literal simply by writing
> "abcd". But if I want write a wide_string literal, how do it?

Same way.  A string literal in quotes can be interpreted as a String,
a Wide_String, a Wide_Wide_String, or any user-defined string type,
depending on context.

> Assume I have two procedure of the form:
>
> procedure do_anything (s : string);
>
> procedure do_anything (w : wide_string);
>
> then
>
> do_anything ("abcd");
>
> would call the first one.

No, it wouldn't.  It would be illegal; since a string literal can be
interpreted as a String or a Wide_String depending on context, the
above is ambiguous because the compiler can't figure out which
do_anything you want.

You can use
      do_anything (String' ("abcd"));
      do_anything (Wide_String' ("abcd"));
to tell the compiler which one you want to call.  Or, since the two
procedures have parameters of different names:
      do_anything (S => "abcd");       -- calls the first one
      do_anything (W => "abcd");       -- calls the second one

> What would one write to call the second
> one?
>
> Is there something like the L"xxx" in C?

Thank God, no.

                                      -- Adam







^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 10:07 String literals and wide_string literals - how? Gerd
  2007-04-20 14:33 ` Adam Beneschan
@ 2007-04-20 14:55 ` Robert A Duff
  2007-04-20 19:16   ` Randy Brukardt
  2007-04-20 22:09   ` Jeffrey R. Carter
  2007-04-20 14:58 ` Georg Bauhaus
  2 siblings, 2 replies; 24+ messages in thread
From: Robert A Duff @ 2007-04-20 14:55 UTC (permalink / raw)


Gerd <GerdM.O@t-online.de> writes:

> Hi all,
>
> if I have a string, then I can write a literal simply by writing
> "abcd". But if I want write a wide_string literal, how do it?

Same syntax.  Context determines the type.

String literals are not necessarily of type String; they can be of any
"string type".  A string type is any one-dimensional array of some
character type.  You can even do weird things like this:

    type Bit is ('0', '1');
    type Bit_String is array (Positive range <>) of Bit;
    pragma Pack(Bit_String);
    X: constant Bit_String := "01011110";

Bit is a character type, and Bit_String is a string type.

X can fit in 8 bits, by the way.

> Assume I have two procedure of the form:
>
> procedure do_anything (s : string);
>
> procedure do_anything (w : wide_string);
>
> then
>
> do_anything ("abcd");
>
> would call the first one.

No, this is ambiguous, and therefore illegal.

>... What would one write to call the second
> one?

To call either one, you need to qualify the type of the parameter:

    Do_Anything(String'("abcd"));
    Do_Anything(Wide_String'("abcd"));
    Blah: Wide_String := "abcd";
    Do_Anything(Blah);

> Is there something like the L"xxx" in C?

That sort of syntax wouldn't work in Ada, because there are an unbounded
number of string types.  The qualified_expression syntax --
"type_name'(expression)" is used instead.

- Bob



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 10:07 String literals and wide_string literals - how? Gerd
  2007-04-20 14:33 ` Adam Beneschan
  2007-04-20 14:55 ` Robert A Duff
@ 2007-04-20 14:58 ` Georg Bauhaus
  2 siblings, 0 replies; 24+ messages in thread
From: Georg Bauhaus @ 2007-04-20 14:58 UTC (permalink / raw)


On Fri, 2007-04-20 at 03:07 -0700, Gerd wrote:
> Hi all,
> 
> if I have a string, then I can write a literal simply by writing
> "abcd". But if I want write a wide_string literal, how do it?


> Is there something like the L"xxx" in C?

   x: Wide_String := Wide_String'("xxx");
   y: Wide_String := "xxx";
   w: Wide_String := ('x', 'x', 'x');
   z: Wide_String := Wide_String'('x', 'x', 'x');

http://en.wikibooks.org/wiki/Ada_Programming/Type_System
and
http://en.wikibooks.org/wiki/Ada_Programming/Types/array
try to explain.



-- Georg 





^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 14:55 ` Robert A Duff
@ 2007-04-20 19:16   ` Randy Brukardt
  2007-04-20 20:01     ` Adam Beneschan
                       ` (2 more replies)
  2007-04-20 22:09   ` Jeffrey R. Carter
  1 sibling, 3 replies; 24+ messages in thread
From: Randy Brukardt @ 2007-04-20 19:16 UTC (permalink / raw)


"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wccbqhjw0we.fsf@shell01.TheWorld.com...
...
> String literals are not necessarily of type String; they can be of any
> "string type".  A string type is any one-dimensional array of some
> character type.  You can even do weird things like this:
>
>     type Bit is ('0', '1');

That's not that weird. My favorite example of this came from an early Ada 83
book:

    type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');
    type Roman_Numeral is array (Positive range <>) of Roman_Digit;
    Five : constant Roman_Numeral := "V";
    Sixteen : constant Roman_Numeral := "XVI";
    My_Age : constant Roman_Numeral := "XLVIII";
    Claw_Generic_Price : constant Roman_Numeral := "CDXCV";
    Claw_Support_Price : constant Roman_Numeral := "CCC";

Now throw in some operator overloading:

    function "+" (Left, Right : Roman_Numeral) return Roman_Numeral;

and you can work solely in Roman_Numerals.

    Total : constant Roman_Numeral := Claw_Generic_Price +
Claw_Support_Price;

Cool, even if rather useless. (I don't think I'd want to try to calculate
sales tax this way!!)

                    Randy.






^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 19:16   ` Randy Brukardt
@ 2007-04-20 20:01     ` Adam Beneschan
  2007-04-20 20:41       ` Dmitry A. Kazakov
  2007-04-20 20:02     ` Robert A Duff
  2007-04-20 20:48     ` Dmitry A. Kazakov
  2 siblings, 1 reply; 24+ messages in thread
From: Adam Beneschan @ 2007-04-20 20:01 UTC (permalink / raw)


On Apr 20, 12:16 pm, "Randy Brukardt" <r...@rrsoftware.com> wrote:

> Now throw in some operator overloading:
>
>     function "+" (Left, Right : Roman_Numeral) return Roman_Numeral;
>
> and you can work solely in Roman_Numerals.

It would be an interesting challenge to write this function---without
cheating.  That is, without resorting to converting the operands to
regular integers, adding them the normal way, and converting the
result back.  No, no, no.  Do it the way the Romans would have had to
do it.  Might make for a good programming exercise.

And then, if someone gets this right, tell them to implement "*".


>     Total : constant Roman_Numeral := Claw_Generic_Price +
> Claw_Support_Price;
>
> Cool, even if rather useless. (I don't think I'd want to try to calculate
> sales tax this way!!)

Given that a lot of people had to calculate a lot of taxes this past
week, I'm sure they're all thankful that they didn't have to fill out
Form MXL this way.

                                         -- Adam






^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 19:16   ` Randy Brukardt
  2007-04-20 20:01     ` Adam Beneschan
@ 2007-04-20 20:02     ` Robert A Duff
  2007-05-21  2:33       ` David Thompson
  2007-04-20 20:48     ` Dmitry A. Kazakov
  2 siblings, 1 reply; 24+ messages in thread
From: Robert A Duff @ 2007-04-20 20:02 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
> news:wccbqhjw0we.fsf@shell01.TheWorld.com...
> ...
>> String literals are not necessarily of type String; they can be of any
>> "string type".  A string type is any one-dimensional array of some
>> character type.  You can even do weird things like this:
>>
>>     type Bit is ('0', '1');
>
> That's not that weird.

OK, it's not weird.  It's quite elegant, in fact, from at least one
point of view -- a character type is "just" an enumeration of the
character literals.

But I'll bet it _seems_ weird to anyone who doesn't know Ada well.  Are
there any other languages that have this sort of thing?  Many folks
presented with the above will assume type Bit above is a subtype of
Character, or derived from it.  That's why I mentioned that
Bit'Size = 1, so the OP would understand that '0' here is not Ascii (or
Latin-1, or whatever) '0'.

And please don't ask me to actually enumerate all the
Wide_Wide_Character literals!

To me, it's weird that in almost all languages, the form of a literal
determines its type.  It seems so much more natural that context should
do so.

>... My favorite example of this came from an early Ada 83
> book:
>
>     type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');
>     type Roman_Numeral is array (Positive range <>) of Roman_Digit;
>     Five : constant Roman_Numeral := "V";
>     Sixteen : constant Roman_Numeral := "XVI";
>     My_Age : constant Roman_Numeral := "XLVIII";
>     Claw_Generic_Price : constant Roman_Numeral := "CDXCV";
>     Claw_Support_Price : constant Roman_Numeral := "CCC";
>
> Now throw in some operator overloading:
>
>     function "+" (Left, Right : Roman_Numeral) return Roman_Numeral;
>
> and you can work solely in Roman_Numerals.
>
>     Total : constant Roman_Numeral := Claw_Generic_Price +
> Claw_Support_Price;
>
> Cool, even if rather useless. (I don't think I'd want to try to calculate
> sales tax this way!!)

;-)

- Bob



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 20:01     ` Adam Beneschan
@ 2007-04-20 20:41       ` Dmitry A. Kazakov
  0 siblings, 0 replies; 24+ messages in thread
From: Dmitry A. Kazakov @ 2007-04-20 20:41 UTC (permalink / raw)


On 20 Apr 2007 13:01:00 -0700, Adam Beneschan wrote:

> On Apr 20, 12:16 pm, "Randy Brukardt" <r...@rrsoftware.com> wrote:
> 
>> Now throw in some operator overloading:
>>
>>     function "+" (Left, Right : Roman_Numeral) return Roman_Numeral;
>>
>> and you can work solely in Roman_Numerals.
> 
> It would be an interesting challenge to write this function---without
> cheating.  That is, without resorting to converting the operands to
> regular integers, adding them the normal way, and converting the
> result back.  No, no, no.  Do it the way the Romans would have had to
> do it.  Might make for a good programming exercise.

Not that hard, Ada has loop in reverse. I think that for summation, even
one character look-ahead is not actually needed (for IV, IX, XL etc stuff).
One should postpone flushing the last "decimal" place before recognizing
the next one, so that gone astray I, X, C could be subtracted from it.

> And then, if someone gets this right, tell them to implement "*".

Romans used abacus for that, they cheated! (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 19:16   ` Randy Brukardt
  2007-04-20 20:01     ` Adam Beneschan
  2007-04-20 20:02     ` Robert A Duff
@ 2007-04-20 20:48     ` Dmitry A. Kazakov
  2 siblings, 0 replies; 24+ messages in thread
From: Dmitry A. Kazakov @ 2007-04-20 20:48 UTC (permalink / raw)


On Fri, 20 Apr 2007 14:16:11 -0500, Randy Brukardt wrote:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
> news:wccbqhjw0we.fsf@shell01.TheWorld.com...
> ...
>> String literals are not necessarily of type String; they can be of any
>> "string type".  A string type is any one-dimensional array of some
>> character type.  You can even do weird things like this:
>>
>>     type Bit is ('0', '1');
> 
> That's not that weird. My favorite example of this came from an early Ada 83
> book:
> 
>     type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');

What about a DNA-sequences?

   type Nucleotide is ('A', 'C', 'G', 'T');
   type DNA_Sequence is array (Positive range <>) of Nucleotide;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 14:55 ` Robert A Duff
  2007-04-20 19:16   ` Randy Brukardt
@ 2007-04-20 22:09   ` Jeffrey R. Carter
  2007-04-21  9:41     ` Simon Wright
  2007-04-22  1:00     ` Robert A Duff
  1 sibling, 2 replies; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-04-20 22:09 UTC (permalink / raw)


Robert A Duff wrote:
> 
>     type Bit is ('0', '1');
>     type Bit_String is array (Positive range <>) of Bit;
>     pragma Pack(Bit_String);
>     X: constant Bit_String := "01011110";
> 
> Bit is a character type, and Bit_String is a string type.
> 
> X can fit in 8 bits, by the way.

for Bit'Size use 1;
for Bit_String'Component_Size use Bit'Size;

subtype Byte_String is Bit_String (1 .. 8);

Y : constant Byte_String := "00001111";
pragma Assert (Y'Size = Y'Length);

type Byte is mod 2 ** 8;
for Byte'Size use 8;

function To_Byte is new Ada.Unchecked_Conversion
    (Source => Byte_String, Target => Byte);

Z : constant Byte := To_Byte (Y);

What is Z and 1?

-- 
Jeff Carter
"You empty-headed animal-food-trough wiper."
Monty Python & the Holy Grail
04



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 22:09   ` Jeffrey R. Carter
@ 2007-04-21  9:41     ` Simon Wright
  2007-04-22  0:35       ` Jeffrey R. Carter
  2007-04-22  1:00     ` Robert A Duff
  1 sibling, 1 reply; 24+ messages in thread
From: Simon Wright @ 2007-04-21  9:41 UTC (permalink / raw)


"Jeffrey R. Carter" <jrcarter@acm.org> writes:

> Robert A Duff wrote:
>>
>>     type Bit is ('0', '1');
>>     type Bit_String is array (Positive range <>) of Bit;
>>     pragma Pack(Bit_String);
>>     X: constant Bit_String := "01011110";
>>
>> Bit is a character type, and Bit_String is a string type.
>>
>> X can fit in 8 bits, by the way.
>
> for Bit'Size use 1;
> for Bit_String'Component_Size use Bit'Size;
>
> subtype Byte_String is Bit_String (1 .. 8);
>
> Y : constant Byte_String := "00001111";
> pragma Assert (Y'Size = Y'Length);
>
> type Byte is mod 2 ** 8;
> for Byte'Size use 8;
>
> function To_Byte is new Ada.Unchecked_Conversion
>    (Source => Byte_String, Target => Byte);
>
> Z : constant Byte := To_Byte (Y);
>
> What is Z and 1?

Well, on a PowerPC Z is 2#00001111# but I suspect you'd get a
different answer on a little-endian machine ..



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-21  9:41     ` Simon Wright
@ 2007-04-22  0:35       ` Jeffrey R. Carter
  2007-04-22  9:45         ` Simon Wright
  0 siblings, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-04-22  0:35 UTC (permalink / raw)


Simon Wright wrote:
> 
> Well, on a PowerPC Z is 2#00001111# but I suspect you'd get a
> different answer on a little-endian machine ..

Actually, that's on a PowerPC with your specific compiler. Another 
compiler might give a different result for the same target. 2#00110011# 
is a perfectly acceptable result. That's why this kind of thing, while 
interesting, is not very useful.

FWIW, with MinGW GNAT 3.4.2 targeting Windows XP on an AMD Turion64 X2, 
Z is 2#11110000#.

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 22:09   ` Jeffrey R. Carter
  2007-04-21  9:41     ` Simon Wright
@ 2007-04-22  1:00     ` Robert A Duff
  1 sibling, 0 replies; 24+ messages in thread
From: Robert A Duff @ 2007-04-22  1:00 UTC (permalink / raw)


"Jeffrey R. Carter" <jrcarter@acm.org> writes:

> Robert A Duff wrote:
>>     type Bit is ('0', '1');
>>     type Bit_String is array (Positive range <>) of Bit;
>>     pragma Pack(Bit_String);
>>     X: constant Bit_String := "01011110";
>> Bit is a character type, and Bit_String is a string type.
>> X can fit in 8 bits, by the way.
>
> for Bit'Size use 1;

Bit'Size is 1 by default, so I suppose:

    pragma Assert(Bit'Size = 1);

would be a better way to express this, in case you care.

> for Bit_String'Component_Size use Bit'Size;
>
> subtype Byte_String is Bit_String (1 .. 8);
>
> Y : constant Byte_String := "00001111";
> pragma Assert (Y'Size = Y'Length);

Y'Size might well be 32 on some implementations.
But that's OK -- Byte_String'Size should be 8.

> type Byte is mod 2 ** 8;
> for Byte'Size use 8;
>
> function To_Byte is new Ada.Unchecked_Conversion
>    (Source => Byte_String, Target => Byte);
>
> Z : constant Byte := To_Byte (Y);
>
> What is Z and 1?

0 or 1, depending on the implementation.

I'm not sure what your point is, here.  Unchecked_Conversion depends on
implementation choices (in this case, based on endianness), and is
therefore implementation defined.  So?

Types Bit and Bit_String above have well-defined portable semantics.
Patient: "Doc, It hurts when I Unchecked_Convert."
Doctor: "So don't do that."  ;-)

- Bob



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-22  0:35       ` Jeffrey R. Carter
@ 2007-04-22  9:45         ` Simon Wright
  2007-04-22 20:15           ` Jeffrey R. Carter
  0 siblings, 1 reply; 24+ messages in thread
From: Simon Wright @ 2007-04-22  9:45 UTC (permalink / raw)


"Jeffrey R. Carter" <jrcarter@acm.org> writes:

> Simon Wright wrote:
>>
>> Well, on a PowerPC Z is 2#00001111# but I suspect you'd get a
>> different answer on a little-endian machine ..
>
> Actually, that's on a PowerPC with your specific compiler. Another
> compiler might give a different result for the same
> target. 2#00110011# is a perfectly acceptable result. That's why
> this kind of thing, while interesting, is not very useful.

It may not be useful but in some applications it's essential to know.

And it seems to me that a compiler that thought 2#00110011# was
acceptable probably wouldn't itself be acceptable.



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-22  9:45         ` Simon Wright
@ 2007-04-22 20:15           ` Jeffrey R. Carter
  2007-04-22 21:18             ` Simon Wright
  0 siblings, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-04-22 20:15 UTC (permalink / raw)


Simon Wright wrote:
> 
> It may not be useful but in some applications it's essential to know.
> 
> And it seems to me that a compiler that thought 2#00110011# was
> acceptable probably wouldn't itself be acceptable.

Why not? It adheres to the well defined and portable semantics Duff 
mentioned.

My point was that these kinds of arrays are promoted as a better way to 
access individual bits than logical operations and shifts. But unless 
you have some definition of which bit "Y (Y'First)" accesses, they're 
not useful for that because they're extremely compiler dependent, while 
logical operations and shifts are portable across compilers and targets. 
Some sort of correspondence with the bit numbers used in record 
representation clauses would be OK.

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-22 20:15           ` Jeffrey R. Carter
@ 2007-04-22 21:18             ` Simon Wright
  2007-04-23  1:44               ` Jeffrey R. Carter
  0 siblings, 1 reply; 24+ messages in thread
From: Simon Wright @ 2007-04-22 21:18 UTC (permalink / raw)


"Jeffrey R. Carter" <jrcarter@acm.org> writes:

> Simon Wright wrote:
>>
>> It may not be useful but in some applications it's essential to know.
>>
>> And it seems to me that a compiler that thought 2#00110011# was
>> acceptable probably wouldn't itself be acceptable.
>
> Why not? It adheres to the well defined and portable semantics Duff
> mentioned.

It just seems wilfully perverse, that's all. What other bizarre
decisions would the designers have made? Why choose software/hardware
that does that if there's a more reasonable alternative?

> My point was that these kinds of arrays are promoted as a better way
> to access individual bits than logical operations and shifts. But
> unless you have some definition of which bit "Y (Y'First)" accesses,
> they're not useful for that because they're extremely compiler
> dependent, while logical operations and shifts are portable across
> compilers and targets. Some sort of correspondence with the bit
> numbers used in record representation clauses would be OK.

I don't quite see the last sentence -- typically the bit numbering
depends on the endianness, very annoying -- not sure whether new Ada
sorts that one out.

For the rest, I quite agree; _much_ easier to map the C condition 
"(x & (1<<2))" to "(X and 2#00000100#) /= 0".



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-22 21:18             ` Simon Wright
@ 2007-04-23  1:44               ` Jeffrey R. Carter
  0 siblings, 0 replies; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-04-23  1:44 UTC (permalink / raw)


Simon Wright wrote:
> 
> I don't quite see the last sentence -- typically the bit numbering
> depends on the endianness, very annoying -- not sure whether new Ada
> sorts that one out.

Ada now recommends that compilers support both bit orders. But even if a 
compiler doesn't, relating the indices of such a packed array to the bit 
numbering scheme for the target means you know what they mean without 
having to experiment.

> For the rest, I quite agree; _much_ easier to map the C condition 
> "(x & (1<<2))" to "(X and 2#00000100#) /= 0".

"X (X'First + 2)" seems easier and clearer to me. Your version is 
preferable because it is portable.

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-04-20 20:02     ` Robert A Duff
@ 2007-05-21  2:33       ` David Thompson
  2007-05-22 22:32         ` Randy Brukardt
                           ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: David Thompson @ 2007-05-21  2:33 UTC (permalink / raw)


On Fri, 20 Apr 2007 16:02:55 -0400, Robert A Duff
<bobduff@shell01.TheWorld.com> wrote:

> "Randy Brukardt" <randy@rrsoftware.com> writes:
<snip>
> OK, it's not weird.  It's quite elegant, in fact, from at least one
> point of view -- a character type is "just" an enumeration of the
> character literals.
> 
> But I'll bet it _seems_ weird to anyone who doesn't know Ada well.  Are
> there any other languages that have this sort of thing?  Many folks
> presented with the above will assume type Bit above is a subtype of
> Character, or derived from it.  That's why I mentioned that
> Bit'Size = 1, so the OP would understand that '0' here is not Ascii (or
> Latin-1, or whatever) '0'.
> 
Pascal does have CHAR as a system-provided enumerated type.
And (normal, nonvarying) STRING as (just) array of char, although it
doesn't have the features for all array types that make Ada string
types (more) convenient.

APL has string as array of char, and a lot of array features which
thus also provide string features. When I used it (some time ago)
those chars were only 'regular' ones; there was not then much interest
in >8b charsets (or really even >6b); don't know if that's changed.

PL/1 does have both char and bit strings builtin, with syntactically
different literals, and I believe they recently have added wide-chars,
but AFAIK not user-defined charsets, and not treated as arrays.

- formerly david.thompson1 || achar(64) || worldnet.att.net



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-05-21  2:33       ` David Thompson
@ 2007-05-22 22:32         ` Randy Brukardt
  2007-05-23 18:42           ` Jeffrey R. Carter
  2007-05-23  2:27         ` Keith Thompson
  2007-05-23 12:28         ` brian.b.mcguinness
  2 siblings, 1 reply; 24+ messages in thread
From: Randy Brukardt @ 2007-05-22 22:32 UTC (permalink / raw)


"David Thompson" <dave.thompson2@verizon.net> wrote in message
news:s71253phu0md9cslvmq2573m2uiqmfu7lo@4ax.com...
> On Fri, 20 Apr 2007 16:02:55 -0400, Robert A Duff
> <bobduff@shell01.TheWorld.com> wrote:
...
> > OK, it's not weird.  It's quite elegant, in fact, from at least one
> > point of view -- a character type is "just" an enumeration of the
> > character literals.
> >
> > But I'll bet it _seems_ weird to anyone who doesn't know Ada well.  Are
> > there any other languages that have this sort of thing?  Many folks
> > presented with the above will assume type Bit above is a subtype of
> > Character, or derived from it.  That's why I mentioned that
> > Bit'Size = 1, so the OP would understand that '0' here is not Ascii (or
> > Latin-1, or whatever) '0'.
> >
> Pascal does have CHAR as a system-provided enumerated type.
> And (normal, nonvarying) STRING as (just) array of char, although it
> doesn't have the features for all array types that make Ada string
> types (more) convenient.

But this isn't relevant; we weren't talking about only about string being
"array of character", but rather that you can define *new* character types
which then can be used in "array of new_char" to get the same behavior as
string (string literals and the like). I'm quite sure Pascal has no similar
facility to that.

My example was:
    type Roman is ('I', 'V', 'X', 'L', 'C');
    type Roman_Numeral is array (Positive range <>) of Roman;
    Eight : Roman_Numeral := "VIII";

"Roman" here is a character type, but there is no way to think of it as a
subtype of "Character" (for one thing, the literals have a different
ordering - 'C' > 'X', for instance). The fact that you can then use this new
type to get something that works like a String is what is interesting.

                             Randy.






^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-05-21  2:33       ` David Thompson
  2007-05-22 22:32         ` Randy Brukardt
@ 2007-05-23  2:27         ` Keith Thompson
  2007-07-01  1:00           ` David Thompson
  2007-05-23 12:28         ` brian.b.mcguinness
  2 siblings, 1 reply; 24+ messages in thread
From: Keith Thompson @ 2007-05-23  2:27 UTC (permalink / raw)


David Thompson <dave.thompson2@verizon.net> writes:
> On Fri, 20 Apr 2007 16:02:55 -0400, Robert A Duff
> <bobduff@shell01.TheWorld.com> wrote:
>> "Randy Brukardt" <randy@rrsoftware.com> writes:
> <snip>
>> OK, it's not weird.  It's quite elegant, in fact, from at least one
>> point of view -- a character type is "just" an enumeration of the
>> character literals.
>> 
>> But I'll bet it _seems_ weird to anyone who doesn't know Ada well.  Are
>> there any other languages that have this sort of thing?  Many folks
>> presented with the above will assume type Bit above is a subtype of
>> Character, or derived from it.  That's why I mentioned that
>> Bit'Size = 1, so the OP would understand that '0' here is not Ascii (or
>> Latin-1, or whatever) '0'.
>> 
> Pascal does have CHAR as a system-provided enumerated type.
> And (normal, nonvarying) STRING as (just) array of char, although it
> doesn't have the features for all array types that make Ada string
> types (more) convenient.
[...]

It's been a while, but as I recall Pascal's CHAR type isn't considered
to be an enumerated type, though it is a scalar type.  And a
user-defined enumerated type can't have character literals as
constants, only identifiers.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-05-21  2:33       ` David Thompson
  2007-05-22 22:32         ` Randy Brukardt
  2007-05-23  2:27         ` Keith Thompson
@ 2007-05-23 12:28         ` brian.b.mcguinness
  2 siblings, 0 replies; 24+ messages in thread
From: brian.b.mcguinness @ 2007-05-23 12:28 UTC (permalink / raw)



> APL has string as array of char, and a lot of array features which
> thus also provide string features. When I used it (some time ago)
> those chars were only 'regular' ones; there was not then much interest
> in >8b charsets (or really even >6b); don't know if that's changed.

APL2 introduced a 31-bit character type over a decade ago.  Other APL
interpreters also support more than 256 characters.

--- Brian




^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-05-22 22:32         ` Randy Brukardt
@ 2007-05-23 18:42           ` Jeffrey R. Carter
       [not found]             ` <f324h5$fna$1@f04n12.cac.psu.edu>
  0 siblings, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-05-23 18:42 UTC (permalink / raw)


Randy Brukardt wrote:
> 
> My example was:
>     type Roman is ('I', 'V', 'X', 'L', 'C');
>     type Roman_Numeral is array (Positive range <>) of Roman;
>     Eight : Roman_Numeral := "VIII";

You forgot 'D' and 'M'!

> "Roman" here is a character type, but there is no way to think of it as a
> subtype of "Character" (for one thing, the literals have a different
> ordering - 'C' > 'X', for instance). The fact that you can then use this new
> type to get something that works like a String is what is interesting.

It's interesting, and fun, and sometimes useful. When using it in the 
latter category, what's irritating is that Ada has no easy way to output 
Eight and other such user-defined character and string values in 
human-readable form. There's no equivalent of the various Text_IO pkgs 
for user-defined character and string types, and Enumeration_IO doesn't 
give you what you want.

-- 
Jeff Carter
"You couldn't catch clap in a brothel, silly English K...niggets."
Monty Python & the Holy Grail
19



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
       [not found]             ` <f324h5$fna$1@f04n12.cac.psu.edu>
@ 2007-05-24 21:15               ` Jeffrey R. Carter
  0 siblings, 0 replies; 24+ messages in thread
From: Jeffrey R. Carter @ 2007-05-24 21:15 UTC (permalink / raw)


Bob Spooner wrote:
>>
> Well, you could overload 'write and then you could put out anything you 
> want.

As I said, there's no easy way ...

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44



^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: String literals and wide_string literals - how?
  2007-05-23  2:27         ` Keith Thompson
@ 2007-07-01  1:00           ` David Thompson
  0 siblings, 0 replies; 24+ messages in thread
From: David Thompson @ 2007-07-01  1:00 UTC (permalink / raw)


On Tue, 22 May 2007 19:27:59 -0700, Keith Thompson <kst-u@mib.org>
wrote:

> David Thompson <dave.thompson2@verizon.net> writes:
<snip>
> > Pascal does have CHAR as a system-provided enumerated type.
> > And (normal, nonvarying) STRING as (just) array of char, although it
> > doesn't have the features for all array types that make Ada string
> > types (more) convenient.
> [...]
> 
> It's been a while, but as I recall Pascal's CHAR type isn't considered
> to be an enumerated type, though it is a scalar type.  And a

I checked and you're right, it's not officially enumerated; it is
however an 'ordinal' type, along with integer boolean and enums, and
you can (and must) SUCC() PRED() and ORD() it.

> user-defined enumerated type can't have character literals as
> constants, only identifiers.

Right. It behaves in what I consider an enumeration-like way, but is
privileged in having character-literal values which user enumeration
types cannot. Of course this is also true in most other languages that
have specific character types: in FORTRAN or COBOL or PL/I 'X' has
character type and you can't treat it as a number at all, although in
the latter two you can get '1' or '123' to be the number _represented_
thereby, not the character code. Only in the C family (which I count
as including Java) is 'X' really a character code.

- formerly david.thompson1 || achar(64) || worldnet.att.net



^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2007-07-01  1:00 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-20 10:07 String literals and wide_string literals - how? Gerd
2007-04-20 14:33 ` Adam Beneschan
2007-04-20 14:55 ` Robert A Duff
2007-04-20 19:16   ` Randy Brukardt
2007-04-20 20:01     ` Adam Beneschan
2007-04-20 20:41       ` Dmitry A. Kazakov
2007-04-20 20:02     ` Robert A Duff
2007-05-21  2:33       ` David Thompson
2007-05-22 22:32         ` Randy Brukardt
2007-05-23 18:42           ` Jeffrey R. Carter
     [not found]             ` <f324h5$fna$1@f04n12.cac.psu.edu>
2007-05-24 21:15               ` Jeffrey R. Carter
2007-05-23  2:27         ` Keith Thompson
2007-07-01  1:00           ` David Thompson
2007-05-23 12:28         ` brian.b.mcguinness
2007-04-20 20:48     ` Dmitry A. Kazakov
2007-04-20 22:09   ` Jeffrey R. Carter
2007-04-21  9:41     ` Simon Wright
2007-04-22  0:35       ` Jeffrey R. Carter
2007-04-22  9:45         ` Simon Wright
2007-04-22 20:15           ` Jeffrey R. Carter
2007-04-22 21:18             ` Simon Wright
2007-04-23  1:44               ` Jeffrey R. Carter
2007-04-22  1:00     ` Robert A Duff
2007-04-20 14:58 ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox