From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6cb2525ffbfe23ce X-Google-Attributes: gid103376,public From: "Pat Rogers" Subject: Re: Why both "with" and "use"? Date: 1999/02/13 Message-ID: <7a4f85$rh1$1@remarQ.com>#1/1 X-Deja-AN: 444032097 References: <36C5B28C.F32C43A4@jps.net> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: newsabuse@remarQ.com X-Trace: 918929477 Y6JRGRJUHDE7CC640C usenet80.supernews.com Organization: Software Arts & Sciences Newsgroups: comp.lang.ada Date: 1999-02-13T00:00:00+00:00 List-Id: 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