Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

ada - Variable initialization in generic package body using return value of function

In Ada, I have the following spec file:

GENERIC
   TYPE Item IS PRIVATE; --type of array
   size : integer; --size of array
   PACKAGE gwar IS
    function get_size return integer;
   END gwar;

And body file:

with Ada.Text_Io;
use Ada.Text_Io;

package body gwar is
   --Get_Size allows the txt file to specify how much space to allocate.
   function get_size return Integer  is
      Filename : String := "win.txt";
      File : Ada.Text_IO.File_Type;
      Line_Count : Integer := 0;
      ReturnSize : Integer;
   begin
      Ada.Text_IO.Open(File => File,
                       Mode => Ada.Text_IO.In_File,
                       Name => Filename);
      while Line_Count /= 1 loop
         declare
            Line : String := Ada.Text_IO.Get_Line(File);
         begin
            ReturnSize := Integer'Value(Line);
            Line_Count := 1;
         end;
      end loop;
      Ada.Text_IO.Close (File);
      return ReturnSize;
   end get_size;

begin
    null;
end gear;

What I want to do is set my size integer to the value returned by get_size. How can I do this? I have tried putting my function before my size variable in the spec file, but it expected end of file. I tried setting size : integer := gwar.get_size, but that does not work either. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Preferably considering manuBriot's remarks, I guess, you could still follow Simon Wright's suggestion technically. I have made a few omission to focus on how to assign a value to a generic parameter in the generic itself.

GENERIC
   TYPE Item IS PRIVATE; --type of array
   size : in out integer; --size of array
PACKAGE gwar IS
   function get_size return integer;
END gwar;

with Ada.Text_Io;
use Ada.Text_Io;

package body gwar is

   function get_size return Integer  is
      ReturnSize : Integer;
   begin
      ReturnSize := Integer'Value("2");
      return ReturnSize;
   end get_size;

begin
   Size := Get_Size;
end gwar;

This way, when you instantiate the generic, the instance body's effect will be to set the parameter size to the value 2, provided that get_size returns without error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...