comp.lang.ada
 help / color / mirror / Atom feed
From: "Pat Rogers" <progers@NOclasswideSPAM.com>
Subject: Re: Why both "with" and "use"?
Date: 1999/02/13
Date: 1999-02-13T00:00:00+00:00	[thread overview]
Message-ID: <7a4f85$rh1$1@remarQ.com> (raw)
In-Reply-To: 36C5B28C.F32C43A4@jps.net

Mike Silva wrote in message <36C5B28C.F32C43A4@jps.net>...
>Somebody on comp.lang.fortran noticed these two lines in an Ada
snippet
>and questioned why both lines were needed (vs. only USE in Fortran).
>
>>with Ada.Numerics.Elementary_Functions;
>>use  Ada.Numerics.Elementary_Functions;
>
>I, with two whole weeks of Ada experience, understand the difference
>(sorta) but don't really understand the rationale.  Would anybody
care
>to explain the why of it?  Thanks.


The "with clause" and "use clause" are part of a "context clause".   I
think of a context clause as meaning "compile the unit in question in
the *context* of these units mentioned in the context clause".

Specifically, the "with clause" means the unit being compiled will
reference the unit named in the clause.  It is somewhat like an
"import" clause in some languages.  In fact, I always say "import"
when I need to use "with" as a verb (e.g., "We need to import
Text_IO.").  Broadly, if a compilation unit references another
independent unit, the "with clause" is required -- the compiler won't
compile the references otherwise.  Mainly one imports packages, but
other program units can be imported as well, especially generic units.
Subprograms can be imported too, although people seem to forget that
subprograms can have separate declarations at the library level.

So if we say:

    with Ada.Text_IO;

we are saying "the compilation unit that follows will make references
to Ada.Text_IO.".

The "use clause", on the other hand, provides *direct* visibility into
the declarations exported from a package, that would otherwise not be
directly visible.  As such, "use clauses" are optional.  Direct
visibility is the purpose of the clause -- without it, the things
inside the imported package can only be referenced indirectly via the
full name, which includes the package name:

    Ada.Text_IO.Put_Line( "This is a call to a procedure named
Put_Line" );

When the "use clause" is applied to Ada.Text_IO, we can reference the
declarations directly, without including the package name:

    Put_Line( "This is a call to a the same procedure named
Put_Line" );

People tend to have strong feelings about whether or not (or how much)
"use clauses" should be applied.  That's a subject for another post.
:-)

Sometimes people get confused when they see both clauses together, as
in

    with Ada.Text_IO;
    use  Ada.Text_IO;

    package Foo is ...

The issue is that "with clauses" connect independent units, and thus
go outside of them ("between them", if you will).  On the other hand,
"use clauses" can be placed *inside* units as well as outside, in both
cases providing direct visibility from that point on down to the end
of the unit (more or less).  Placing a "use clause" outside of the
compilation unit (i.e. right after the "with clause") just means that
direct visibility to the exported declarations is available throughout
the compilation unit, as if we had placed it immediately within the
compilation unit before anything else.

    with Ada.Text_IO;

    package Foo is

        use  Ada.Text_IO;

        ...

    end Foo;

So, the two clauses serve different purposes, and have different
placement requirements, but are related because they allow and control
visibility -- indirect visibility via "with clauses", and direct
visibility via "use clauses".  One is required, the other optional.

Hope that helps,

---
Pat Rogers                          Training & Development in:
http://www.classwide.com    Deadline Schedulability Analysis
progers@acm.org                 Software Fault Tolerance
(281)648-3165                       Real-Time/OO Languages






  parent reply	other threads:[~1999-02-13  0:00 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-13  0:00 Why both "with" and "use"? Mike Silva
1999-02-13  0:00 ` Corey Minyard
1999-02-13  0:00   ` Matthew Heaney
1999-02-13  0:00     ` bill
1999-02-14  0:00       ` dewar
1999-02-14  0:00       ` Matthew Heaney
1999-02-14  0:00       ` dewar
1999-02-13  0:00     ` Tom Moran
1999-02-14  0:00       ` Matthew Heaney
1999-02-16  0:00     ` Samuel Mize
1999-02-17  0:00       ` Jean-Pierre Rosen
1999-02-18  0:00         ` robert_dewar
1999-02-18  0:00           ` Jean-Pierre Rosen
1999-02-18  0:00             ` robert_dewar
1999-02-19  0:00               ` Jean-Pierre Rosen
1999-02-18  0:00         ` dennison
1999-02-18  0:00           ` robert_dewar
1999-02-19  0:00             ` dennison
1999-02-19  0:00             ` Robert I. Eachus
1999-02-19  0:00               ` robert_dewar
1999-02-23  0:00                 ` Robert I. Eachus
1999-02-19  0:00               ` Brian Hanson
1999-02-19  0:00                 ` robert_dewar
1999-02-19  0:00             ` bourguet
1999-02-19  0:00               ` robert_dewar
1999-02-19  0:00           ` Matthew Heaney
1999-02-19  0:00             ` dennison
1999-02-19  0:00               ` robert_dewar
1999-02-19  0:00                 ` Ada multiple string personalities. why so many? mike
1999-02-19  0:00                   ` Tom Moran
1999-02-19  0:00                     ` Mike Silva
1999-02-22  0:00                       ` Brian Hanson
1999-02-19  0:00                   ` robert_dewar
1999-02-19  0:00                 ` Why both "with" and "use"? dennison
1999-02-19  0:00                   ` robert_dewar
1999-02-17  0:00       ` Matthew Heaney
1999-02-17  0:00       ` Jean-Pierre Rosen
1999-02-17  0:00       ` dennison
1999-02-17  0:00         ` Nick Roberts
1999-02-17  0:00         ` Samuel Mize
1999-02-13  0:00   ` mike
1999-02-13  0:00 ` Pat Rogers [this message]
1999-02-13  0:00   ` bill
1999-02-13  0:00     ` Pat Rogers
1999-02-13  0:00       ` Matthew Heaney
1999-02-13  0:00         ` bill
1999-02-14  0:00         ` Pat Rogers
1999-02-14  0:00           ` Bob Collins
1999-02-14  0:00             ` Pat Rogers
1999-02-16  0:00         ` Samuel Mize
1999-02-13  0:00     ` Matthew Heaney
1999-02-15  0:00     ` Jean-Pierre Rosen
1999-02-15  0:00       ` Ed Falis
1999-02-16  0:00         ` Jean-Pierre Rosen
1999-02-15  0:00 ` Jean-Pierre Rosen
replies disabled

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