comp.lang.ada
 help / color / mirror / Atom feed
From: stefan-lucks@see-the.signature
Subject: Re: using `use' : what is the best practice ?
Date: Wed, 8 Jun 2011 08:06:58 +0200
Date: 2011-06-08T08:06:58+02:00	[thread overview]
Message-ID: <Pine.LNX.4.64.1106080735330.20422@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <57e75a35-058c-4172-9a3d-e11e7c5f7697@l2g2000prg.googlegroups.com>

On Tue, 7 Jun 2011, milouz wrote:

> Maybe a not so anecdotic question : when using the 'use' clause ?

I am sure you will get a lot of different answers about this. 

Note that your example below is using "use" in the "context clause" (i.e., 
where the "with" is). 

>    with P1; use P1;
>    with P2; use P2;
> 
>    procedure Main is
>       My_Val : Integer;
>    begin
>       Foo(My_Val);  -- P1 or P2 ?
>    ...

Try out what your program does! If P1 defines 
  procedure Foo(I: Integer); 
and P2 defines only, say, 
  Procedure Foo(S: String);
then the Ada compiler will know that you are calling P1.Foo. You are 
allowed to write P1.Foo(My_Val) to improve the readability of your code. 

On the other hand, if P2 also defines 
  procedure Foo(I: Integer);
you *must* either write P1.Foo(My_Val) or P2.Foo(My_Val). The compiler 
will not try to "guess" which Foo you wanted to call, it will just give 
you the syntax error you deserve. 

> But always avoiding 'use' give some rather obfuscated code, especially
> when using some specific arithmetic operator. For example, playing
> with Address arithmetic without 'use' :

There are alternatives to the use in the syntax clause:
  -> use type;
  -> a renames clause 
  -> a local use 

>    with System;
>    with System.Storage_Elements;
> 
>    procedure Main is
>       A : System.Address;
>       N : Integer;

        use System.Storage_Elements;
        -- locally in any scope -- not very local if in the main procedure
 
        use type System.Address;
        -- alternative: use type

        package SSE renames System.Storage_Elements;
        -- alternative: renames

>    begin
>       A := System.Storage_Elements.To_Address(16#10000#);

        -- A := To_Address(...); -- for local use (or in context clause)
                                 -- but not for use type!

        -- A := SSE.To_Address(...); -- for renames

>       N := 4;
>  
> A := System.Storage_Elements."+"(System.Storage_Elements.Storage_Offset(N), A);

  -- A := SSE."+"(SSE.Storage_Offset(N), A); -- for renames

  -- A := Storage_Offset(N)+ A; -- for local use, context-clause use 
                                -- and use type

>    end Main;
> 
> As I'm very new, I'd like to have your opinion about the way to use
> 'use'.

Always consider the alternatives to the context-clause use!


-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------



  parent reply	other threads:[~2011-06-08  6:06 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-07 15:32 using `use' : what is the best practice ? milouz
2011-06-07 15:59 ` Simon Wright
2011-06-07 16:22 ` Dmitry A. Kazakov
2011-06-07 16:59   ` Simon Wright
2011-06-07 17:25     ` Dmitry A. Kazakov
2011-06-07 17:29       ` Simon Wright
2011-06-07 19:38         ` Dmitry A. Kazakov
2011-06-11 14:11           ` Yannick Duchêne (Hibou57)
2011-06-08  9:07       ` Paul Colin Gloster
2011-06-08 11:20         ` stefan-lucks
2011-06-08 16:45           ` Vinzent Hoefler
2011-06-11 14:05       ` Yannick Duchêne (Hibou57)
2011-06-07 17:33 ` Pascal Obry
2011-06-08  2:42   ` Randy Brukardt
2011-06-12  5:09   ` Yannick Duchêne (Hibou57)
2011-06-07 18:51 ` Jeffrey Carter
2011-06-08  0:04   ` Peter C. Chapin
2011-06-07 19:32 ` Anders Wirzenius
2011-06-07 23:38 ` Shark8
2011-06-12  5:19   ` Yannick Duchêne (Hibou57)
2011-06-12  7:55   ` Stephen Leake
2011-06-08  4:25 ` Jerry
2011-06-08  9:31   ` milouz
2011-06-08 17:17     ` Ludovic Brenta
2011-06-08 18:43     ` Jeffrey Carter
2011-06-09 10:40       ` Address arithmetic alternatives milouz
2011-06-09 11:25         ` Georg Bauhaus
2011-06-09 18:53         ` Jeffrey Carter
2011-06-08 19:22     ` using `use' : what is the best practice ? Pascal Obry
2011-06-09  1:17     ` Shark8
2011-06-09 10:44       ` milouz
2011-06-08  6:06 ` stefan-lucks [this message]
2011-06-15  2:25 ` Adrian Hoe
replies disabled

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