Use a package spec inside a procedure - ada

I want to use a package spec inside a procedure.
Something is missing for that, but i don't know what.
using_ads_package.adb:14:11: "var" is not visible
using_ads_package.adb:14:11: non-visible declaration at line 8
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
Procedure using_ads_package is
Package variable is
var : Unbounded_String ;
end variable ;
Begin
get_line(var);
End using_ads_package ;

In
procedure Using_Ads_Package is
package Variable is
Var : Unbounded_String;
end Variable;
begin
you’ve created a new scope in package Variable. Outside Variable, Var isn’t directly visible.
Outside Variable, you have two ways of referring to Var.
First, you can name the package in which it’s declared explicitly:
Get_Line (Variable.Var);
Alternatively, you can use Variable:
package Variable is
Var : Unbounded_String;
end Variable;
use Variable;
begin
Get_Line (Var);

Without compiling I’d suggest that the line:
get_line(var);
should be:
get_line(variable.var);

Related

Ada : custom type in range 1..var?

I'm a very beginner of Ada code. I use GPS from AdaCore.
I would create a variable sized by the user.
I write this :
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure main is
wanted : Integer := 10;
type custom is range 0..wanted;
...
But something went wrong in line 8 :
Builder results
C:\Users\**********\Desktop\ada project\src\main.adb
8:26 "wanted" is not static constant or named number (RM 4.9(5))
8:26 non-static expression used for integer type bound
I really don't understand what this mean... Can someone help me ?
Variable wanted is not a constant, it may change its value during program execution, therefore this variable is not allowed to be used as range constraint when declaring new types. You may however make it constant by using constant keyword (Wanted : constant Integer := 10;). It should resolve your problem.
As said by Timur, wanted has to be constant in its scope. This allows you some nice things such as declaring a type inside a procedure. Look at this, it might be of interest :)
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Main is
procedure Test (Wanted : Integer) is
type custom is new Integer range 0..wanted;
begin
Put_Line("First value " & Custom'Image (Custom'First)
& " Last value " & Custom'Image (Custom'Last));
end Test;
begin
Test (10);
Test (12);
end Main;
Output is
First value 0 Last value 10
First value 0 Last value 12
In this case, your type is different from one call to another but it works as wanted is constant within the procedure. The only thing is that the type defined has to be a new derived type of the type of your parameter.
I let you think about the possibilities :)

Can't understand how generics work

I have a package called Linked_List(.ads) Here is the code in it:
Generic
type T is private;
package Linked_List is
type List is tagged record
Data : T;
end record;
end Linked_List;
and here is the code in the package which contains the main function (main.adb)
with Ada.Text_IO; use Ada.Text_IO;
with Linked_List;
procedure Main is
type ListOfIntegers is new Linked_List(T => Integer);
begin
null;
end Main;
I'm getting this error:
4:30 subtype mark required in this context
found "Linked_List" declared at linked_list.ads:3
found "Linked_List" declared at linked_list.ads:3
4:41 incorrect constrain for this kind of type
Any help is appreciated.
new Linked_List(T => Integer) defines a new package, not a new type. The error messages you’re getting are because the compiler thinks you’re declaring a type, so seeing the name of a package at column 30 confused it; it wanted to see the name of a (sub)type.
Line 4 should read
package ListOfIntegers is new Linked_List(T => Integer);
after which there is a type ListOfIntegers.List, so you can write
My_List : ListOfIntegers.List;
You may find having to say ListOfIntegers. all the time annoying; you can say
use ListOfIntegers;
after which you can just write
My_List : List;
but it’s usually thought best not to overdo this (if you have dozens of “withed” packages, “using” them all makes it difficult to know which one you’re referring to).
By the way, normal Ada usage is to use underscores to separate words in identifiers: List_Of_Names.

ada - declarations must come before begin?

Question: why and how to fix this error message about ADA? Thax
12:04 declarations must come before "begin"
29:01 statement expected
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
With Ada.Strings.Unbounded; Use Ada.Strings.Unbounded;
With Ada.Strings.Bounded;
procedure polynomial is
begin
function evaluate (x: Integer) return Integer is
type i is new Integer;
type p is new Integer;
type x is new Integer;
begin
for i in reverse 0..10 loop
i:= i-1;
p:= coef(i)+x*p;
end loop;
return p;
end evaluate;
end polynomial;
As the error message says, declarations must come before begin. Thus, if you have a declaration, it must come before the begin of the construct that encloses it. For example, the type declarations of i, x, and p (which should not be type declarations, but that's another problem) are directly enclosed in the evaluate function. Therefore, they must appear before the begin of the evaluate function, as they are.
The thing is that this applies to function and procedure declarations, too--and a complete function body, such as evaluate, counts as a declaration for this rule. evaluate is directly enclosed in polynomial. Therefore, it must appear before the begin line belonging to polynomial, whereas your code puts it right after the begin line, which is illegal.
There are actually two ways to fix it: (1) Move evaluate before the begin line. (2) Add a block that begins with declare:
procedure polynomial is
begin
declare
function evaluate (x: Integer) return Integer is
i : Integer;
-- etc.
begin
-- etc.
end evaluate;
begin -- this is the BEGIN line of the block
-- Now put some code here!
end; -- this is the end of the block
end polynomial;
Now the evaluate function is directly enclosed in the block, not in polynomial, and it must occur before the begin line of the block, but it doesn't have to occur before the begin line of polynomial.
A couple other things: This declaration:
type i is new Integer;
does not mean that "the type of i is an integer". It means you're declaring a new type named i. You don't want that, you want a variable.
i : Integer;
Second: note the part of the block where I said "now put some code here". That's necessary. Declaring the function evaluate does not run the function. You have to put in a statement that calls it, if you ever want your function to run. Your original incorrect code didn't have any statements that called evaluate, which is why I am not sure whether you understood that concept.

How do I use “separate" keyword

I am unable to figure out keyword separate in Ada and its depth concept. Please help me to understand by giving a small example?
Lets say I have a nested procedure
with ada.text_io; use ada.text_io;
procedure main is
procedure proc is
begin
put_line ("i am proc");
end proc;
begin
put_line ("main");
end main;
How to use separate keyword ?
You primarily use the separate keyword to achieve one of 2 effects.
OS specific actions. (Put 2 versions of the procedure / functions in different directories, and compile for 2 different targets)
Separation of a lengthy procedure from surrounding code.
Here is an example to show the syntax.
package_x.ads
package Package_X is
procedure Foo;
procedure Sep;
end Package_X;
package_x.adb
package body Package_X is
procedure Foo is
begin
null;
end Foo;
procedure Sep is separate;
end Package_X;
package_x-sep.adb
separate (Package_X) procedure Sep is
begin
null;
end Sep;
The separate keyword creates a unit of compilation, a subunit, that is compiled independently. The parameter of separate refers to the package in which the subunit is a sub unit of.
So if you had a package body X, then you remove procedure Y from it, you create a sub unit of X by creating a new file in which you place Y, and put "separate(X)" at the start of the file, to indicate that Y is really part of X.

"Unbounded" is not visible in Ada

I am just a beginner in Ada,
my code looks like this,
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
procedure final is
Input : Unbounded.String;
begin
null;
end final;
When i compile this code with gnatmake,
compiler gives error "Unbounded" is not visible", what does this mean?
The use clause for Ada.Strings.Unbounded makes declarations inside that package directly visible. It does not make the package name itself (Unbounded) directly visible, so any reference to the name Unbounded that's not preceded by a . is going to be incorrect.
Furthermore, the type is called Unbounded_String, not String.
Change this:
Input : Unbounded.String;
to this:
Input : Unbounded_String;
(And please indent your code.)

Resources