From: Anders Wirzenius <anders.wirzenius@netikka.fi>
Subject: Re: using `use' : what is the best practice ?
Date: Tue, 07 Jun 2011 22:32:54 +0300
Date: 2011-06-07T22:32:54+03:00 [thread overview]
Message-ID: <87hb818mjd.fsf@cawu.ok8> (raw)
In-Reply-To: 57e75a35-058c-4172-9a3d-e11e7c5f7697@l2g2000prg.googlegroups.com
milouz <a.michelizza@gmail.com> writes:
> Hi guys,
> Sorry with my annoying questions, but it's not easy to program in Ada
> after 20 years programming in asm / C ;-)
>
> Maybe a not so anecdotic question : when using the 'use' clause ?
>
> My first thought is that using 'use' is not good because it masks the
> package tree.
> For example, if I have 2 packages with procedures sharing the same
> name, example `P1.Foo' and `P2.Foo', my code won't be very readable if
> I only write the short name (`Foo') :
>
> with P1; use P1;
> with P2; use P2;
>
> procedure Main is
> My_Val : Integer;
> begin
> Foo(My_Val); -- P1 or P2 ?
> ...
>
> But always avoiding 'use' give some rather obfuscated code, especially
> when using some specific arithmetic operator. For example, playing
> with Address arithmetic without 'use' :
>
> with System;
> with System.Storage_Elements;
>
> procedure Main is
> A : System.Address;
> N : Integer;
> begin
> A := System.Storage_Elements.To_Address(16#10000#);
> N := 4;
>
> System.Storage_Elements."+"(System.Storage_Elements.Storage_Offset(N),
> A);
> end Main;
>
> The same code with 'use' is more readable :
>
> with System; use System;
> with System.Storage_Elements; use System.Storage_Elements;
>
> procedure Main is
> A : Address;
> N : Integer;
> begin
> A := To_Address(16#10000#);
> N := 4;
> A := A + Storage_Offset(N);
> end Main;
>
> As I'm very new, I'd like to have your opinion about the way to use
> 'use'.
>
>
>
You may restrict the context of a 'use':
with Text_IO;
procedure Bl is
A : String := "aaa";
begin
Text_IO.Put_Line (A);
declare
use Text_IO;
A : character := 'b';
begin
Put (A);
end;
Put_Line (A);
end;
Try to compile the code. The error message (Gnat) will be:
gnatmake bl.adb
gcc-4.4 -c bl.adb
bl.adb:12:04: "Put_Line" is not visible
bl.adb:12:04: non-visible declaration at a-textio.ads:263
bl.adb:12:04: non-visible declaration at a-textio.ads:259
gnatmake: "bl.adb" compilation error
--
Anders
next prev parent reply other threads:[~2011-06-07 19:32 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 [this message]
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
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