comp.lang.ada
 help / color / mirror / Atom feed
* use Ada.Text_IO in main() or Package?
@ 2017-09-14  5:09 Mace Ayres
  2017-09-14  5:12 ` Mace Ayres
  2017-09-14  6:21 ` gautier_niouzes
  0 siblings, 2 replies; 10+ messages in thread
From: Mace Ayres @ 2017-09-14  5:09 UTC (permalink / raw)


I am moving code I had in main() into a package and getting errors.
In main(), I had with Ada.Text_IO and use Ada.Text_IO and all worked OK.
When moving code into a package, to be called from Main(),
     with and use clauses in Main() are not seen in Package code & adding with and use Ada-Text_IO in package throws error "with can only appear in context clause"
I get lost here.

- --------------------------
package body structures is  
   
 with Ada.Text_IO;
 use  Ada.Text_IO;

   
 PROCEDURE set_up is         --------------------------------
      
with  Ada.Text_IO; use Ada.Text_IO;   -- throws error, without it errors in put_line and other Ada.Text_IO functions..
type grid is array (1..9) of Integer;
layer_1: grid:=(1,2,3,4,5,6,7,8,9);
   
begin
New_Line(2);
   Put_Line("Hello Michael from Ada");
   Put_Line(sayhi("Hi"));  -- parameter not used in structures::say; it uses its own
   New_Line;
   for i of layer_1 loop
      Put ("Layer 1 grid one dimension has values:" );
      Put(layer_1(i),Width=>2);       -- Width :=3));
          New_Line;
   end loop;
   New_line(2);      
end set_up;  ----------------------------------------------- 
   
    


^ permalink raw reply	[flat|nested] 10+ messages in thread

* use Ada.Text_IO in main() or Package?
  2017-09-14  5:09 use Ada.Text_IO in main() or Package? Mace Ayres
@ 2017-09-14  5:12 ` Mace Ayres
  2017-09-14  6:33   ` Petter Fryklund
  2017-09-14  6:21 ` gautier_niouzes
  1 sibling, 1 reply; 10+ messages in thread
From: Mace Ayres @ 2017-09-14  5:12 UTC (permalink / raw)


Correction: using with and use AdA.TEXT_IO before in within package function both don't work. I don 't use it in both places.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  5:09 use Ada.Text_IO in main() or Package? Mace Ayres
  2017-09-14  5:12 ` Mace Ayres
@ 2017-09-14  6:21 ` gautier_niouzes
  2017-09-14  6:47   ` Mace Ayres
  1 sibling, 1 reply; 10+ messages in thread
From: gautier_niouzes @ 2017-09-14  6:21 UTC (permalink / raw)


The "with" clauses are always on top of the text, before anything (package, procedure, function).
A "use" clause opens the visibility ("use Ada.Text_IO" allows you to write "Put" instead of "Ada.Text_IO.Put"), so it can be in the same region as "with", or more locally, inside a function for instance. Sometimes it is more practical to restrict the visibility. In a complex program, you may want to have always a package name prefixed, like "Display.Line" for readability, but in some function that displays plenty of things, it will be more readable to have a local "use Display;", then your bunch of Line(...); Circle(...); etc. are obviously from "Display".
HTH
Gautier
_____________________________________________________________
A free online game in Ada: http://pasta.phyrama.com/game.html


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  5:12 ` Mace Ayres
@ 2017-09-14  6:33   ` Petter Fryklund
  2017-09-14  6:51     ` Mace Ayres
  0 siblings, 1 reply; 10+ messages in thread
From: Petter Fryklund @ 2017-09-14  6:33 UTC (permalink / raw)


Den torsdag 14 september 2017 kl. 07:12:32 UTC+2 skrev Mace Ayres:
> Correction: using with and use AdA.TEXT_IO before in within package function both don't work. I don 't use it in both places.

I would do it like this:

with Ada.Text_IO; 
with Where_Ever_Say_Hi_Lives;
package body Structures is   
        
 procedure Set_Up is         -------------------------------- 
    
   type grid is array (1..9) of Integer; 

   layer_1 : grid := (1,2,3,4,5,6,7,8,9); 
    
begin 
  Ada.Text_IO.New_Line(2); 
  Ada.Text_IO.Put_Line("Hello Michael from Ada"); 
  Ada.Text_IO.Put_Line(Where_Ever_Say_Hi_Lives.Say_Hi("Hi"));
  Ada.Text_IO.New_Line; 
   
  for I of Layer_1 loop 
    Ada.Text_IO.Put("Layer 1 grid one dimension has values:" ); 
    Ada.Text_IO.Put(layer_1(I), Width => 2);       -- Width :=3)); 
    Ada.Text_IO.New_Line; 
   end loop; 
   Ada.Text_IO.New_line(2);       
end Set_Up;  ----------------------------------------------- 

But I am of old school. I don't use use, I use package renamings in a small scope. 

https://books.google.se/books?id=iPZGJRW9bKgC&pg=PA44&lpg=PA44&dq=write+once+read+many+programming+style&source=bl&ots=Dt_NIoNyK5&sig=QKkQWPIatt0HeyVcl6BXeRv3Lpo&hl=sv&sa=X&ved=0ahUKEwjr5NnphqTWAhXDFZoKHUqLBucQ6AEIdjAJ#v=onepage&q=write%20once%20read%20many%20programming%20style&f=false

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  6:21 ` gautier_niouzes
@ 2017-09-14  6:47   ` Mace Ayres
  2017-09-14  7:13     ` gautier_niouzes
  0 siblings, 1 reply; 10+ messages in thread
From: Mace Ayres @ 2017-09-14  6:47 UTC (permalink / raw)


Thanks Gautier: So, the with and use Ada.Text_IO in the Main() function, is not visible inside the package that is also with and used  in main().

 I have to put the with and use Ada.Text_IO again in the package, because it calls some of ...text_IO functions within the package body?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  6:33   ` Petter Fryklund
@ 2017-09-14  6:51     ` Mace Ayres
  2017-09-15  5:22       ` Petter Fryklund
  0 siblings, 1 reply; 10+ messages in thread
From: Mace Ayres @ 2017-09-14  6:51 UTC (permalink / raw)


Thanks,  When you say old school, do you mean that's how you did it in earlier versions when it was needed, or you like the clarity of using the prefix?

Aso, is it true that in the end, after the compiler does its thing, the end result in object or machine code is the same, as unnecessary stuff is stripped out as the compiler simplifies and these package references are meaningless in final machine object code, more or less?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  6:47   ` Mace Ayres
@ 2017-09-14  7:13     ` gautier_niouzes
  2017-09-14  9:37       ` Mace Ayres
  0 siblings, 1 reply; 10+ messages in thread
From: gautier_niouzes @ 2017-09-14  7:13 UTC (permalink / raw)


Le jeudi 14 septembre 2017 08:47:35 UTC+2, Mace Ayres a écrit :
> Thanks Gautier: So, the with and use Ada.Text_IO in the Main() function, is not visible inside the package that is also with and used  in main().

That's it (fortunately). By the way it's the same in all sane languages.
Your package Structures is autonomous and doesn't know of Main().
G.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  7:13     ` gautier_niouzes
@ 2017-09-14  9:37       ` Mace Ayres
  2017-09-14  9:49         ` gautier_niouzes
  0 siblings, 1 reply; 10+ messages in thread
From: Mace Ayres @ 2017-09-14  9:37 UTC (permalink / raw)


Tks. It does make sense. I haven't been programming much in last decades, never really go into OOP much, but I can see this is going to avoid a lot of issues, and enforces some rigor.

BTW, have you work with gtkAda at all? I am trying to set it up for user GUI interface on my Mac OS X where I have GNAT and GPS running.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  9:37       ` Mace Ayres
@ 2017-09-14  9:49         ` gautier_niouzes
  0 siblings, 0 replies; 10+ messages in thread
From: gautier_niouzes @ 2017-09-14  9:49 UTC (permalink / raw)


Le jeudi 14 septembre 2017 11:37:41 UTC+2, Mace Ayres a écrit :
> Tks. It does make sense. I haven't been programming much in last decades, never really go into OOP much, but I can see this is going to avoid a lot of issues, and enforces some rigor.

Actually it is just a question of what is depending on what.
Instead of Structures, you can take Ada.Text_IO itself as an example.
In the guts of package Ada.Text_IO, the GNAT run-time library has a package body file: a-textio.adb) with the following lines:

    with Ada.Streams;             use Ada.Streams;
    with Interfaces.C_Streams;    use Interfaces.C_Streams;

    with System.File_IO;
    with System.CRTL;
    with System.WCh_Cnv;          use System.WCh_Cnv;
    with System.WCh_Con;          use System.WCh_Con;

    with Ada.Unchecked_Conversion;
    with Ada.Unchecked_Deallocation;

It makes sense they are there and not expected to be in any Main() or any other referencing Ada.Text_IO.
I've never used System.File_IO for instance and would not: it is GNAT only and unknown in other Ada systems. And there is no need for that.
G.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: use Ada.Text_IO in main() or Package?
  2017-09-14  6:51     ` Mace Ayres
@ 2017-09-15  5:22       ` Petter Fryklund
  0 siblings, 0 replies; 10+ messages in thread
From: Petter Fryklund @ 2017-09-15  5:22 UTC (permalink / raw)


I still have to maintain some very old code, that is perhaps one reason, but with completions available in the editors I use, it is not many more characters to write. 

When using emacs, navigation is not that simple, so I use unix find command to look for references. Then the full name is use full.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-09-15  5:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-14  5:09 use Ada.Text_IO in main() or Package? Mace Ayres
2017-09-14  5:12 ` Mace Ayres
2017-09-14  6:33   ` Petter Fryklund
2017-09-14  6:51     ` Mace Ayres
2017-09-15  5:22       ` Petter Fryklund
2017-09-14  6:21 ` gautier_niouzes
2017-09-14  6:47   ` Mace Ayres
2017-09-14  7:13     ` gautier_niouzes
2017-09-14  9:37       ` Mace Ayres
2017-09-14  9:49         ` gautier_niouzes

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