'with'ing and 'use'ing 
Author Message
 'with'ing and 'use'ing

Hi,

I'm just trying to clear something up in my head.

I'm trying to interface with a lot [a *lot* :] of Ada95 code and the rest
of the code has a policy of not 'use'ing other packages, just 'with'ing
them to force the full names of types & functions etc. As a result, I'm
adopting the same attitude.

1) Is this basically a good idea? It seems sensible to me, but then I know
   little about Ada. (I'm asking about generally not 'use'ing stuff, not
   specifically my current situation)

2) One of the packages defines a sub-package called Ops, which contains
   the operators (=,/=,>=,<=) etc for some of the types in the main
   package. The only way I seem to be able to get access to these operators
   is to 'use' the package. Is this right, or am I missing somehting?

Any help gratefully received.

Thanks

rog.



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing
Actually, the idea of not "Use"ing packages is quite common.  But you
are right, the effect of not pulling in the operators for defined types
can get a bit irritating.  My project saw this early on and allows the
use of the "use type xxxx;" statement to circumvent the need for the
functional notation use of operators such as ">=" etc...  The 'use type'
statement has the effect of providing visibility to all operations on
the type specified without haveing to resort to a general use clause on
the entire package surrounding the type in question.  Quite handy and
much more readable.

Robert

Quote:

> Hi,

> I'm just trying to clear something up in my head.

> I'm trying to interface with a lot [a *lot* :] of Ada95 code and the rest
> of the code has a policy of not 'use'ing other packages, just 'with'ing
> them to force the full names of types & functions etc. As a result, I'm
> adopting the same attitude.

> 1) Is this basically a good idea? It seems sensible to me, but then I know
>    little about Ada. (I'm asking about generally not 'use'ing stuff, not
>    specifically my current situation)

> 2) One of the packages defines a sub-package called Ops, which contains
>    the operators (=,/=,>=,<=) etc for some of the types in the main
>    package. The only way I seem to be able to get access to these operators
>    is to 'use' the package. Is this right, or am I missing somehting?

> Any help gratefully received.

> Thanks

> rog.



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:
> of the code has a policy of not 'use'ing other packages, just
> 'with'ing them to force the full names of types & functions etc. As a
> result, I'm adopting the same attitude.

> 1) Is this basically a good idea? It seems sensible to me, but then I
>    know little about Ada. (I'm asking about generally not 'use'ing
>    stuff, not specifically my current situation)

That's a very contraversial issue (odds are, you've just unwittingly
touched off another flamewar about it). Suffice it to say that many
people (myself included) think that's the "right" way to do things. Many
other people whose opinions I respect feel that's hogwash.

The basic issue is how easy it is for a new reader (such as yourself) to
figure out where routines and objects are declared. If you have source
code analysis tools available that can easily find declarations in other
source files for you whenever you need to know, then there's a good
argument that "use"s are OK. If you have some routines for which it is
obvious to any competent reader where the declarations are, then it
could be argued that "use"s are OK.

Quote:
> 2) One of the packages defines a sub-package called Ops, which
>    contains the operators (=,/=,>=,<=) etc for some of the types in
>    the main package. The only way I seem to be able to get access to
>    these operators is to 'use' the package. Is this right, or am I
>     missing somehting?

That's one of 2 idioms that were commonly used in Ada83 to gain
visibility to operators. The other was to do a function rename for them
somewhere in the scope you wanted to use them.

In Ada 95 you don't really need to do either anymore. There is a "use
type" clause that serves this purpose. Unlike the normal "use" clause,
"use type" is not very contraversial.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:

> 1) Is this basically a good idea? It seems sensible to me, but then I know
>    little about Ada. (I'm asking about generally not 'use'ing stuff, not
>    specifically my current situation)

You've just started a language war between the two dialects of Ada --
the use-phobes and the use-philes.  ;-)

And then there's the folks who advocate renaming all packages as
incomprehensible abbreviations, so you can say XQYBC.Grind_Upon_List.  ;-)

I slightly prefer using use clauses.

Of course, you don't have much choice -- the existing code doesn't use
use, so presumably the names were chosen with that in mind.

Quote:
>    the operators (=,/=,>=,<=) etc for some of the types in the main
>    package. The only way I seem to be able to get access to these operators
>    is to 'use' the package. Is this right, or am I missing somehting?

This is a trick used in Ada 83 to avoid use-visibility for identifiers,
but still allow the operators to be directly visible.  (While saying
``Foo.Bar(X, Y)'' might be reasonable, saying ``Foo."+"(X, Y)'' is an
abomination.)  Yes, you need to say "use Whatever.Ops;" for this trick
to work.  The trick is no longer needed in Ada 95 -- use "use type"
instead.

The trick is a little bit dangerous, because the code in the Ops package
is so boring that the reader assumes it says the obvious, without
reading it carefully.  I remember spending some time tracking down a bug
caused by saying something like ``function "xor"(...) renames "and";''
-- a cut-and-paste error.

- Bob



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:

> Actually, the idea of not "Use"ing packages is quite common.  But you
> are right, the effect of not pulling in the operators for defined types
> can get a bit irritating.  My project saw this early on and allows the
> use of the "use type xxxx;" statement to circumvent the need for the
> functional notation use of operators such as ">=" etc...  The 'use type'
> statement has the effect of providing visibility to all operations on
> the type specified without haveing to resort to a general use clause on
> the entire package surrounding the type in question.  Quite handy and
> much more readable.

This is incorrect: "use type" does not provide visibility to "all
operations". Use type provides visibility to operators; operations that
are not operators still require dot notation.

Jeff Carter



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:

> Hi,

> I'm just trying to clear something up in my head.

> I'm trying to interface with a lot [a *lot* :] of Ada95 code and the rest
> of the code has a policy of not 'use'ing other packages, just 'with'ing
> them to force the full names of types & functions etc. As a result, I'm
> adopting the same attitude.

> 1) Is this basically a good idea? It seems sensible to me, but then I know
>    little about Ada. (I'm asking about generally not 'use'ing stuff, not
>    specifically my current situation)

Whether using a package is a good thing depends solely on how easy it is
for someone unfamiliar with the code to read or modify it. Some packages
are designed to be used; see, for example, Ada.Strings.Unbounded. This
package was designed to be used; the type is named Unbounded_String, and
"Unbounded" shows up in many names (Null_Unbounded_String,
To_Unbounded_String). This makes it easy to tell where things came from.

Other packages are so common that it doesn't affect readability to use
them; examples include Ada.Text_IO and
Ada.Numerics.Elementary_Functions. If you see "Put_Line" or "Sqrt" in
your code it's pretty obvious where they come from.

Other packages are not designed to be used, or are designed not to be
used. I tend to write data structure packages so that the code has
declarations such as

   Frequency : Frequency_List.Handle;
   Pending   : Event_Queue.Handle;

These type names would be ambiguous without the dot notation if you used
both packages, and since they both have "Get" operations, the code can
be difficult to read. You probably shouldn't use these packages.

Some people (and organizations) have strict "don't use" policies, while
others use pretty much everything. I advocate pragmatism based on ease
of reading and modifying the code.

Jeff Carter



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:

> The trick is a little bit dangerous, because the code in the Ops
> package is so boring that the reader assumes it says the obvious,
> without reading it carefully.  I remember spending some time tracking
> down a bug caused by saying something like ``function "xor"(...)
> renames "and";'' -- a cut-and-paste error.

I can vouch from personal experience that the *other* Ada83 trick
(renaming the functions) is succeptable to that exact same bug. :-(

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:


> > Hi,

> > I'm just trying to clear something up in my head.

> > I'm trying to interface with a lot [a *lot* :] of Ada95 code and the rest
> > of the code has a policy of not 'use'ing other packages, just 'with'ing
> > them to force the full names of types & functions etc. As a result, I'm
> > adopting the same attitude.

> > 1) Is this basically a good idea? It seems sensible to me, but then I know
> >    little about Ada. (I'm asking about generally not 'use'ing stuff, not
> >    specifically my current situation)

> Whether using a package is a good thing depends solely on how easy it is
> for someone unfamiliar with the code to read or modify it. Some packages
> are designed to be used; see, for example, Ada.Strings.Unbounded. This
> package was designed to be used; the type is named Unbounded_String, and
> "Unbounded" shows up in many names (Null_Unbounded_String,
> To_Unbounded_String). This makes it easy to tell where things came from.

> Other packages are so common that it doesn't affect readability to use
> them; examples include Ada.Text_IO and
> Ada.Numerics.Elementary_Functions. If you see "Put_Line" or "Sqrt" in
> your code it's pretty obvious where they come from.

> Other packages are not designed to be used, or are designed not to be
> used. I tend to write data structure packages so that the code has
> declarations such as

>    Frequency : Frequency_List.Handle;
>    Pending   : Event_Queue.Handle;

I use a very similar style when now myself, with the main type exported
from a package getting the name "T" and helper types getting names like
"Index_T". One of the things that motivated me to try that style was a
deliberate attempt to program without use clauses.

I use use clauses but I thought it would be a good exercise to try and
program without them.

Quote:
> These type names would be ambiguous without the dot notation if you used
> both packages, and since they both have "Get" operations, the code can
> be difficult to read. You probably shouldn't use these packages.

> Some people (and organizations) have strict "don't use" policies, while
> others use pretty much everything. I advocate pragmatism based on ease
> of reading and modifying the code.

I agree completely with this sentiment, and the rest of the post. Draconian
rules like "never use use clauses, goto, exceptions for control flow, ATC,
subprogram nesting, etc" seem silly to me. Understand the motivations for
the proposed restrictions, and decide for yourself based on the situation.

-- Brian



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing
Sorry... Operat'ors'.
Quote:


> > Actually, the idea of not "Use"ing packages is quite common.  But you
> > are right, the effect of not pulling in the operators for defined types
> > can get a bit irritating.  My project saw this early on and allows the
> > use of the "use type xxxx;" statement to circumvent the need for the
> > functional notation use of operators such as ">=" etc...  The 'use type'
> > statement has the effect of providing visibility to all operations on
> > the type specified without haveing to resort to a general use clause on
> > the entire package surrounding the type in question.  Quite handy and
> > much more readable.

> This is incorrect: "use type" does not provide visibility to "all
> operations". Use type provides visibility to operators; operations that
> are not operators still require dot notation.

> Jeff Carter



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:
>2) One of the packages defines a sub-package called Ops, which contains
>   the operators (=,/=,>=,<=) etc for some of the types in the main
>   package. The only way I seem to be able to get access to these operators
>   is to 'use' the package. Is this right, or am I missing somehting?

I'm surprised no one has mentioned it, but you can always call them
as Foo."="(a, b) without needing any form of use. I would recommend
"use type" instead, though, since you aren't bound from above. =
is a lot more elegant than Foo."=", and rarely makes it less readable.
(Of course, I probably overuse just plain "use"s, so take that with
a grain of salt.)

--

Only a nerd would worry about wrong parentheses with
square brackets. But that's what mathematicians are.
   -- Dr. Burchard, math professor at OSU



Sat, 17 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing

Quote:
>                            My project saw this early on and allows the
> use of the "use type xxxx;" statement to circumvent the need for the
> functional notation use of operators such as ">=" etc...  The 'use type'
> statement has the effect of providing visibility to all operations on
> the type specified without haveing to resort to a general use clause on
> the entire package surrounding the type in question.  Quite handy and
> much more readable.

Oooh, that works a treat.

Thanks a lot.

cheers

rog.

(currently a happy rabbit)



Sun, 18 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:
> That's a very contraversial issue (odds are, you've just
unwittingly
> touched off another flamewar about it). Suffice it to say that
many
> people (myself included) think that's the "right" way to do
things. Many
> other people whose opinions I respect feel that's hogwash.

So, when you are using existing packages that are already
existing, the choice of whether to use USE or not is often
dictated by the naming conventions of that package. When you
design a package, you choose names that are designed either
with the idea of USE in mind or not.

It is really quite irritating to see a package that was
carefully designed to be used with USE adopted into a program
which blindly follows the no-USE policy, and end up with noise
like:

   Angle_Handling.Angle_Handling_Options := True;

As always, blind rules are just that, blind :-)

Whether it is a good idea or not to avoid USE depends on many
things, but the basic design point of view is to make things
easier for the reader. This may partially depend on the
environment. For example, if the readers have to suffer in
an environment (they still exist :-( today) where there is
no simple way to click on a name to immediately find out what
package it is defined in, then putting dots all over the place
may have some advantage in some cases for readers.

What some people like to do is to use local abbreviations

  package MCA renames Major_Complex_Arithmetic;

and then use MCA.xxx in the client. This to me seems the worst
of both worlds, because now even a complete familiarity with
the package is not enough to guess what is going on. If you
must follow this approach, please standardize the abbreviations
to be used and document them in the package itself, so that
all clients use the same abbreviation.

Indeed if everyone everywhere will use MCA as the "name" of
this package, perhaps you should define a library level
renaming of the package and with that, or even change the
name of the package to MCA.

My own taste is for a flexible approach. Use USE where it is
helpful to do so, don't use it otherwise, don't have fixed
rules (which are seldom a good substitute for judgment).

The policy in the GNAT sources is to use USE in the compiler
itself, it makes the code a lot clearer once you get to know
the basic structure of the sources, since in 99% of the cases
you immediately know where something is. For example, even a
surface familiarity of the GNAT sources will tell you that

  Expand_N_Goto_Statement;

must be in the package Exp_Ch5, since it is obviously an
expansion activity, and we know goto's are a statement form
defined in chapter 5. Furthermore, we assume that typically
if you are working with the GNAT sources, you have reasonable
locating tools available (such as are supplied with GNAT).

For the run-time library, we mostly avoid the use of USE, since
these sources are used in all kinds of contexts, and typically
and frequently examined by people not very familiar with the
library.

It is relatively unusual (did any user ever do it?) to dip into
the GNAT front end sources to answer a question about GNAT. It
is quite common to dip into the runtime for this purpose.

Robert Dewar
Ada Core Technologies

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 18 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:

> > 1) Is this basically a good idea? It seems sensible to me,
but then I know
> >    little about Ada. (I'm asking about generally not
'use'ing stuff, not
> >    specifically my current situation)

> You've just started a language war between the two dialects of
Ada --
> the use-phobes and the use-philes.  ;-)

That's not quite right, because the situation is not
symmetrical.

The use-phobes are indeed phobic, and actually subset the
language, often working in a subset that forbids USE clauses
completely.

The class of people who Bob calls use-philes are no more
use-philes than they are procedure-files or
assignment-statement-philes. It is not that they *like*
USE clauses, they simply regard them, like any other
feature in the language, as something to be used when
appropriate and not otherwise.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 18 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:

> It is really quite irritating to see a package that was
> carefully designed to be used with USE adopted into a program
> which blindly follows the no-USE policy, and end up with noise
> like:

>    Angle_Handling.Angle_Handling_Options := True;

As someone who would probably end up writing the above, this is a
statement I can agree whole-heartedly with. There are legitimate
examples where object names in a package can be well-designed to support
use's. In fact I saw a very good example of that in another followup of
yours. But this is not one of them. You can't possibly argue that

   Angle_Handling_Options := True;

is easier to understand than

   Angle_Handling.Options := True;

which is how it could have been named. The only difference is a single
character, and the first contains *less* information for the reader.

I just don't see what good is gained by taking this kind of naming
approach. Its as if the author was thinking: "People may do a "use",
obfuscating the origin of this object. So I'll partially thwart them by
tacking the package name to the front of the object again". Never mind
that the users of this package will now be forced to type the stupidly
redundant statement at the top, or to perform a "use" clause regardless
of whether it is appropriate in their environment.

I wouldn't bother to drag the thread off on this tangent, except that
this brand of user-callousness is rife in the Ada.* packages, and it is,
as you say, quite irritating. Worse yet is the thought that some people
may take up this style for their own general-purpose Ada packages.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 18 Aug 2002 03:00:00 GMT  
 'with'ing and 'use'ing


Quote:
> But this is not one of them. You can't possibly argue that

>    Angle_Handling_Options := True;

> is easier to understand than

>    Angle_Handling.Options := True;

> which is how it could have been named.

I don't! But equally arguing that the second is easier to
understand than the first is also a heavy bruden. They
are really pretty much equivalent in the environment where
you know that Angle_Handling options is a package, and
either is preferable in my view to

 ANHN.Options := True;

What happens is in practice is that you use compound names
some of the time but not all. Unbounded strings in Annex A
is a perfect example of the kind of naming I think is quite
appropriate for a package intended to be used (as someone
else pointed out on this list).

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 18 Aug 2002 03:00:00 GMT  
 
 [ 55 post ]  Go to page: [1] [2] [3] [4]

 Relevant Pages 

1. un-'upvar'ing/un-'global'ing variables

2. FTP'ing and FAX'ing from within C4b

3. 'make'ing ghc on darwin

4. 'call'ing a variable

5. Expect: Trouble 'expect'ing linefeeds

6. Ping'ing w/o plugins

7. C5EE GOLD - GPF'ing should read C55EE Gold

8. C5EE GOLD -W2K- GPF'ing with Large Dict - MSQL

9. Why GPF'ing in CW20?

10. Changing property of Control USE'ing Array Element

11. My opinion about ' ing constants

12. Request: Example of AppleScript'ing of lvsbutil.app

 

 
Powered by phpBB® Forum Software