comp.lang.ada
 help / color / mirror / Atom feed
From: anon@att.net
Subject: Re: Position of "use"
Date: Wed, 9 Jul 2014 10:36:09 +0000 (UTC)
Date: 2014-07-09T10:36:09+00:00	[thread overview]
Message-ID: <lpj5uo$kd1$1@speranza.aioe.org> (raw)
In-Reply-To: lp86r0$ssl$1@speranza.aioe.org



Using a global "use_clause" can sometime cause the compiler to hide all 
routines of one or more libraries. It is better to place most use_clause 
in a local routine or within the declare section of a block_statement.

--
--  Example File: Test_2.adb
--
--  Showing where compiler could hide visibility access to packages.
--
with Ada.Text_IO ;  --  There are a number of routines that are 
with GNAT.IO ;      --  common in both packages so, placement of
                    --  the use_clause is critical, if used.

procedure body Test_2 is 

--  Both Ada.Text_IO and GNAT.IO packages must be access by using 
--  "selected_component" at this point

  procedure Test_2b is 
    use GNAT.IO ; --  Grants global visibility to package GNAT.IO. but
                  --  limits Ada.Text_IO to a "selected_component" 
                  --  access
    begin
      null ;
    end ;

  --  Allow all following routines will have visibility to 
  --  Ada.Text_IO could also cause visibility problems with other 
  --  packages.
  use  Ada.Text_IO ; 

  procedure Test_2a is 
      use GNAT.IO ; --  Will cause the compiler to hide packages, 
                    --  so in this example any reference must be by 
                    --  "selected_component" for both Ada.Text_IO
                    --  and GNAT.IO packages
    begin
      null ;
    end ;

begin
  null ;
end ;


For Ada compilers, where you can have more than one compilation_unit 
within one file (RM 10.1 -- 10.1.1), it allows all preceding 
compilation_unit to have access the same library with only one 
"with_clause" without sharing global visibility to that package.

Note: GNAT limits the compilation_unit to one per file.  But GNAT still 
allows this type of program design. Putting the "with_clause" in the 
specification file (.ads) allows access for the corresponding body file 
(.adb) in some cases without having the "with_clause" in the body file.


--
--  Example of a multiple compilation_unit File: Test_Lib.ada
--
--  Showing different access/visibility to package Text_IO

--
--  compilation_unit 1 : Access to Text_IO is forbidden in this 
--                       package
package Test_Init is
--  ...
end Test_Init ;
--
with Text_IO ;  -- Access is Allowed
--  use Text_IO ;  --  Visibility would be global to all
--                 --  specification and body units if this 
--                 --  statement is used.

--
--  compilation_unit 2 : Limits visibility to Text_IO to a 
--                       "selected_component", unless the 
--                       global "use_clause" is used.
--
--              example :  Text_IO.Col 
--
package Test_55 is
  procedure Init ;
--  ...
end Test_55 ;
--
--
--  compilation_unit 3 : Visibility of package depend upon 
--                       placement of the "use_clause"
--
package body Test_55 is

--  use Text_IO ;  -- Visibility is global within this package body

  procedure Init is
      use Text_IO ;  --  Visibility is local to procedure
    begin
      Set_Col ( 5 ) ; 
      Set_Line ( 2 ) ; 
    end Init ;

  procedure Error is
    begin
      declare
        use Text_IO ;  --  Visibility is local to this block statement
      begin
       Put ( "Error" ) ;
      end ;
    end Error ;
--    ...
end Test_55 ;
--

--
--  compilation_unit 4 : Limits visibility to Text_IO to a 
--                       "selected_component", unless the 
--                       global "use_clause" is used.
--
--              example :  Text_IO.New_Line ;
--
package Test_Cal is
--    ...
end Test_Cal ;



In <lp86r0$ssl$1@speranza.aioe.org>, Victor Porton <porton@narod.ru> writes:
>What is the difference between
>
>with X; use X;
>package Y is
>end Y;
>
>and
>
>with X;
>package Y is
>   use X;
>end Y;
>
>?
>
>-- 
>Victor Porton - http://portonvictor.org

  parent reply	other threads:[~2014-07-09 10:36 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-05  6:43 Position of "use" Victor Porton
2014-07-05  7:12 ` J-P. Rosen
2014-07-07 16:45   ` Adam Beneschan
2014-07-07 17:08     ` Pascal Obry
2014-07-07 17:40       ` Peter Chapin
2014-07-07 19:17       ` Adam Beneschan
2014-07-08  5:26     ` J-P. Rosen
2014-07-08 15:32       ` Adam Beneschan
2014-07-08 19:30 ` Adam Beneschan
2014-07-08 22:39   ` Victor Porton
2014-07-09 10:36 ` anon [this message]
2014-07-09 15:14   ` Adam Beneschan
2014-07-10  1:27     ` anon
2014-07-10  9:50       ` AdaMagica
2014-07-10 13:10         ` J-P. Rosen
2014-07-10 15:57           ` Adam Beneschan
2014-07-10 17:47             ` Tero Koskinen
2014-07-10 19:15           ` Jeffrey Carter
2014-07-15  5:56         ` anon
2014-07-15  7:36           ` Georg Bauhaus
2014-07-15 17:01             ` Simon Wright
2014-07-15 17:23               ` Jeffrey Carter
2014-07-15 19:44                 ` Simon Wright
2014-07-15 17:47               ` G.B.
2014-07-15 17:51               ` Adam Beneschan
2014-07-15 20:04                 ` Simon Wright
2014-07-16  7:19                 ` anon
2014-07-10 15:54       ` Adam Beneschan
replies disabled

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