comp.lang.ada
 help / color / mirror / Atom feed
* Ambiguous expressions - Help!
@ 2013-09-24 17:38 Mike H
  2013-09-24 18:15 ` Eryndlia Mavourneen
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Mike H @ 2013-09-24 17:38 UTC (permalink / raw)


package Foo1 is
   type grid_index_type  is new integer range 0..80;
   type grid_type is array (grid_index_type range <>) of character;
   subtype Vector_type is grid_type (0 .. 8);
... etc.

with Foo1;
package Foo2 is
   procedure Put_line (Item : in Foo1.Vector_type);
   procedure Put_line (Item : in string);
... etc.

with Foo1; use Foo1;
with Foo2;
procedure Foo3 is
   Foo_thing : Vector_type;
begin
... etc.
   Foo2.Put_line (Foo_thing);
... etc.
   Foo2.Put_line ("Something else");
... etc.
end Foo3;

Compilation of Foo3 raises error messages of the form "ambiguous
expression (cannot resolve "Put_line")

I find it hard to believe that the compiler cannot distinguish between a
string and an array of characters. What am I doing wrong?
-- 
Mike
Swim? Naturally at Severn Vale
<http://www.severnvalesc.org/>


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

* Re: Ambiguous expressions - Help!
  2013-09-24 17:38 Ambiguous expressions - Help! Mike H
@ 2013-09-24 18:15 ` Eryndlia Mavourneen
  2013-09-24 18:43   ` Adam Beneschan
  2013-09-24 18:37 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Eryndlia Mavourneen @ 2013-09-24 18:15 UTC (permalink / raw)


On Tuesday, September 24, 2013 12:38:34 PM UTC-5, Mike H wrote:
> package Foo1 is
>    type grid_index_type  is new integer range 0..80;
>    type grid_type is array (grid_index_type range <>) of character;
>    subtype Vector_type is grid_type (0 .. 8);
> ... etc.
> 
> with Foo1;
> package Foo2 is
>    procedure Put_line (Item : in Foo1.Vector_type);
>    procedure Put_line (Item : in string);
> ... etc.
> 
> with Foo1; use Foo1;
> with Foo2;
> procedure Foo3 is
>    Foo_thing : Vector_type;
> begin
> ... etc.
>    Foo2.Put_line (Foo_thing);
> ... etc.
>    Foo2.Put_line ("Something else");
> ... etc.
> end Foo3;
> 
> Compilation of Foo3 raises error messages of the form "ambiguous
> expression (cannot resolve "Put_line")
> 
> I find it hard to believe that the compiler cannot distinguish between a
> string and an array of characters. What am I doing wrong?
> -- 
> Mike

An Ada String *is* an array of characters.  I didn't realize it, but apparently the String constant (in quotes) can be converted automatically into any vector of characters, even those not declared explicitly as "String"s.

--Eryndlia (KK1T)


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

* Re: Ambiguous expressions - Help!
  2013-09-24 17:38 Ambiguous expressions - Help! Mike H
  2013-09-24 18:15 ` Eryndlia Mavourneen
@ 2013-09-24 18:37 ` Jeffrey Carter
  2013-09-24 21:08 ` Georg Bauhaus
  2013-09-25  7:14 ` Mike H
  3 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2013-09-24 18:37 UTC (permalink / raw)


On 09/24/2013 10:38 AM, Mike H wrote:
>
> Compilation of Foo3 raises error messages of the form "ambiguous
> expression (cannot resolve "Put_line")
>
> I find it hard to believe that the compiler cannot distinguish between a
> string and an array of characters. What am I doing wrong?

Any one-dimensional array with components of a character type is a string type. 
Every string type has corresponding literals. String is a string type, but one 
can declare other string types.

Grid_Type is a string type, and "Something else" may be a literal of type 
Grid_Type. It might also be a literal of type String. Hence the compiler cannot 
determine which Put_Line is intended. You can help it by writing

Foo2.Put_Line (Item => Foo1.Grid_Type'("Something else") );

(unless you meant the other Put_Line).

-- 
Jeff Carter
"He didn't get that nose from playing ping-pong."
Never Give a Sucker an Even Break
110

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

* Re: Ambiguous expressions - Help!
  2013-09-24 18:15 ` Eryndlia Mavourneen
@ 2013-09-24 18:43   ` Adam Beneschan
  2013-09-24 20:34     ` Robert A Duff
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2013-09-24 18:43 UTC (permalink / raw)


On Tuesday, September 24, 2013 11:15:35 AM UTC-7, Eryndlia Mavourneen wrote:
> On Tuesday, September 24, 2013 12:38:34 PM UTC-5, Mike H wrote:

> > I find it hard to believe that the compiler cannot distinguish between a
> > string and an array of characters. What am I doing wrong?

> An Ada String *is* an array of characters.  I didn't realize it, but apparently the String constant (in quotes) can be converted automatically into any vector of characters, even those not declared explicitly as "String"s.

A string literal can actually be converted to any array of an enumeration type, if the enumeration type has at least one character literal as one of its literals.  The classic example is:

    type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');
    type Roman_Numeral is array (Natural range <>) of Roman_Digit;
    R : Roman_Numeral := "MMXIII";

Roman_Numeral is called a "string type".  String, Wide_String, and Wide_Wide_String are other string types, as is Foo1.Grid_Type in the question.
When a string literal can be converted to more than one string type, it's ambiguous.  This does cause problems when there are two overloaded subprograms where one has a String parameter and another has a Wide_String or Wide_Wide_String parameter; the use of the string literal is ambiguous.  As Jeff answered, you have to specify which type you want:

    Foo2.Put_Line (Item => String'("Something else"));
    Foo2.Put_Line (Item => Foo1.Grid_Type' ("Something else"));

                                   -- Adam



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

* Re: Ambiguous expressions - Help!
  2013-09-24 18:43   ` Adam Beneschan
@ 2013-09-24 20:34     ` Robert A Duff
  0 siblings, 0 replies; 7+ messages in thread
From: Robert A Duff @ 2013-09-24 20:34 UTC (permalink / raw)


Adam Beneschan <adambeneschan@aol.com> writes:

> A string literal can actually be converted to any array of an
> enumeration type, if the enumeration type has at least one character
> literal as one of its literals.  The classic example is:
>
>     type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');
>     type Roman_Numeral is array (Natural range <>) of Roman_Digit;
>     R : Roman_Numeral := "MMXIII";

Right, and it's interesting that Roman_Digit'Size = 3 bits,
and the representation of 'I' is 0, 'V' is 1, ..., 'M' is 6.

- Bob

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

* Re: Ambiguous expressions - Help!
  2013-09-24 17:38 Ambiguous expressions - Help! Mike H
  2013-09-24 18:15 ` Eryndlia Mavourneen
  2013-09-24 18:37 ` Jeffrey Carter
@ 2013-09-24 21:08 ` Georg Bauhaus
  2013-09-25  7:14 ` Mike H
  3 siblings, 0 replies; 7+ messages in thread
From: Georg Bauhaus @ 2013-09-24 21:08 UTC (permalink / raw)


On 24.09.13 19:38, Mike H wrote:

> procedure Foo3 is
>     Foo_thing : Vector_type;
> begin
> ... etc.
>     Foo2.Put_line (Foo_thing);
> ... etc.
>     Foo2.Put_line ("Something else");
> ... etc.
> end Foo3;
>
> Compilation of Foo3 raises error messages of the form "ambiguous
> expression (cannot resolve "Put_line")

"Something else" is a literal of either type.
Putting a qualifying type name in front removes the ambiguity,

   Put_line (String'("Something else"));


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

* Re: Ambiguous expressions - Help!
  2013-09-24 17:38 Ambiguous expressions - Help! Mike H
                   ` (2 preceding siblings ...)
  2013-09-24 21:08 ` Georg Bauhaus
@ 2013-09-25  7:14 ` Mike H
  3 siblings, 0 replies; 7+ messages in thread
From: Mike H @ 2013-09-25  7:14 UTC (permalink / raw)


One of the pleasant things about this particular corner of news net is 
that replies to queries can be expected to be helpful and informative. 
Many thanks, I now know a lot more about strings and arrays!

In the mean time my simplistic solution has been to avoid overloading by 
changing the declaration
with Foo1;
package Foo2 is
    procedure Put_line (Item : in Foo1.Vector_type);
    procedure Put_line (Item : in string);
... etc.

to
with Foo1;
package Foo2 is
    procedure Put_vector (Item : in Foo1.Vector_type);
    procedure Put_line (Item : in string);
... etc.

-- 
The thing I like best about the Internet is that no one
knows that, in reality, I am just another old dog!
Mike

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

end of thread, other threads:[~2013-09-25  7:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-24 17:38 Ambiguous expressions - Help! Mike H
2013-09-24 18:15 ` Eryndlia Mavourneen
2013-09-24 18:43   ` Adam Beneschan
2013-09-24 20:34     ` Robert A Duff
2013-09-24 18:37 ` Jeffrey Carter
2013-09-24 21:08 ` Georg Bauhaus
2013-09-25  7:14 ` Mike H

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