comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos" <david.c.hoos.sr@ada95.com>
To: <And838N@netscape.net>, <comp.lang.ada@ada.eu.org>
Subject: Re: visibility
Date: Thu, 5 Jun 2003 10:16:03 -0500
Date: 2003-06-05T10:16:03-05:00	[thread overview]
Message-ID: <mailman.6.1054826185.289.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: 666191F6.00B406DF.0015D3EC@netscape.net


----- Original Message ----- 
From: <And838N@netscape.net>
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, June 04, 2003 5:12 PM
Subject: visibility


> Ok, here's another question.  I'll provide some background information
first.
>
> Let us say that there is a procedure in an .adb file that is not in a
> package and it looks something like the following:
>
> ----------------------
> with
> ada.text_io,
> ada.integer_text_io,
> ada.strings.unbounded;
>
> use
> ada.text_io,
> ada.integer_text_io,
> ada.strings.unbounded;
>
> procedure x is
>     astring: unbounded_string := "hello world";
>     somenumber: integer := 0;
> begin
>     somenumber := count(astring, to_set(' '));
>     put(somenumber);
> end x;
> -----------------------
>
> Now we go compile it and GNAT gives me a message that says there is
> more than one "count" and that I have to specify, by using the full
> package name before the word count, in order to specify which count I
> want.  Which isn't so bad, yet...

First of all, gnat dows not report what you said it reports; here's the
actual report:
x.adb:12:34: expected private type "Ada.Strings.Unbounded.Unbounded_String"
x.adb:12:34: found a string type
x.adb:15:19: "count" is not visible
x.adb:15:19: multiple use clauses cause hiding
x.adb:15:19: hidden declaration at a-strunb.ads:217
x.adb:15:19: hidden declaration at a-strunb.ads:211
x.adb:15:19: hidden declaration at a-strunb.ads:205
x.adb:15:19: hidden declaration at a-textio.ads:66
x.adb:15:34: "to_set" is not visible
x.adb:15:34: non-visible declaration at a-strmap.ads:107
x.adb:15:34: non-visible declaration at a-strmap.ads:105
x.adb:15:34: non-visible declaration at a-strmap.ads:70
x.adb:15:34: non-visible declaration at a-strmap.ads:68

As is usual, the gnat error messages are very clear and precise.  You
need to eliminate the errors in order.  Changing the declaration of
"astring" to a correct one soves that problem.  E.g. change it to:

astring: Unbounded_String := To_Unbounded_String ("hello world");

Ada does not do implicit conversions -- i.e. it does not "automagically"
do things behind the scenes that might not be what you intended.

The second error -- the invisibility of "count" is discussed below.

The third error -- the invisibility of "to_set" is corrected by including
"ada.strings.maps" in your "with" and "use" clauses.
>
> So out of curiosity I look at the version of count inside ada.text_io
> and compare it to the version of count inside ada.strings.unbounded.
> Turns out the count inside ada.text_io isn't a "procedure" at all, it's
> a variable type.

Why did you "with" and "use" ada.text_io, when your program doesn't
need them?  Elimination of this error solves the problem of visibility
of "count".

>
> So I investigate further.  The author of Ada as a second language,
> Norman Cohen provided a chapter on scope and visiblity.  In that
> chapter (actually an appendix), he says that one of the only times
> there will be a visibility problem is when a variable type and a
> procedure have the same name.

I could find no such statement on my copy of Cohen's book.

He does say that "hiding" should be avoided, so you should have heeded the
compiler message.

>
> It is hard for me to accept that Ada (GNAT) can't tell the difference
> between a variable type and a procedure?  I wouldn't be surprised if
> both counts were procedures or both counts were variable types, having the
same "signature".  But a variable type compared to a procedure?
The "count" you are using in your program is not a procedure; it's a
function.
The "count" in Ada.Text_IO is a type (there's no such thing as a "variable
type" in Ada.
Ada types can be used to declare constants as well as variables.

When a type name appears after ":=" -- i.e., in an expression -- the
compiler would
think it was a type conversion.  Type conversions have the same syntax as a
function call.

> Does that mean that after a visibility conflict occurs when compiling
> that there is no "signature" checking? Can someone please explain?
>
> Second question related to "with"ing and "use"ing. (I think the first
> question might be resolved by re-working my "with"s and "use"s.)
> If I "with" ada.strings, that makes it, and it's children "visible" to
> my procedure or package?

No. The children are not visible. You must explicitly "with" the children
you will use.

> If so, then I can "use" only the children I want, like fixed,
> unbounded, unbounded_text_io, etcetera?

There is no such unit as "unbounded_text_io" in the Ada standard libraries.

> If so, does the compiler or linker still link in, or for lack of a
> better term "insert" code into my procedure/package for the children I
> didn't "use"?

"Use" clauses affect only visibility; they have no effect on linking.
All compilation units "withed" are linked into the executable, regardless of
whether
they are referenced in your program.
>
> Thanks,
>
> Andrew
>
> P.S.
> How's the word wrap now?
>
>
>
> __________________________________________________________________
> McAfee VirusScan Online from the Netscape Network.
> Comprehensive protection for your entire computer. Get your free trial
today!
> http://channels.netscape.com/ns/computing/mcafee/index.jsp?promo=393397
>
> Get AOL Instant Messenger 5.1 free of charge.  Download Now!
> http://aim.aol.com/aimnew/Aim/register.adp?promo=380455
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




       reply	other threads:[~2003-06-05 15:16 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <666191F6.00B406DF.0015D3EC@netscape.net>
2003-06-05 15:16 ` David C. Hoos [this message]
     [not found] <009C830A.36D4A463.0015D3EC@netscape.net>
2003-06-07 12:18 ` Visibility David C. Hoos, Sr.
2003-06-08  3:28   ` Visibility Hyman Rosen
2003-06-08 10:20     ` Visibility Frank J. Lhota
2003-06-08 16:18       ` Visibility Robert I. Eachus
2003-06-07  8:21 Visibility And838N
2003-06-07 17:29 ` Visibility Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2003-06-06  5:15 Visibility christoph.grein
2003-06-04 22:12 visibility And838N
2003-06-05 10:35 ` visibility Preben Randhol
2003-06-05 15:19 ` visibility Frank J. Lhota
1992-12-24 11:48 Visibility agate!spool.mu.edu!darwin.sura.net!Sirius.dfn.de!chx400!sicsun!disuns2!lg
1992-12-24  2:16 Visibility Richard Pattis
1992-12-23 23:35 Visibility agate!spool.mu.edu!uwm.edu!cs.utexas.edu!torn!nott!netfs!news
replies disabled

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