1
votes

Page 53 of "Programming in Ada 2012" by John Barnes shares an incomplete fragment of code that I cannot get to work.

I've come up with this as a full program expanding the code from the book...

with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
  type Coin is (Heads, Tails);
  package Random_Coin is new Discrete_Random(Coin);
  use Random_Coin;

  G : Generator;
  C : Coin;
begin

  for i in 1 .. 20 loop
    C := Random(G);
    Put (C'Image);
  end loop;  

end Main;

The "GPS" IDE I'm using complains of the following errors:

  • Line 6: "Discrete_Random" is undefined
  • Line 7: "Random_Coin" is undefined
  • Line 9: "Generator" is undefined
  • Line 14: "Random" is undefined

The IDE does give me the "intellisense" (to use a term from Visual Studio) that indicates that Discrete_Random is actually visible and available given the "with" and "use" statements I've added.

Can someone walk me through the stupid mistakes I'm making please?

1
with Ada.Numerics.Discrete_Random; - egilhh
p.s. what are people's opinions of this book? I'm having real problems learning Ada, using this as the main reference. - user5069935
@egilhh, I've tried a few variations, none of them work, although sometimes it changes from "undefined" to "not visible". - user5069935
@Wossname egilh just told you what is the correct name for that package. See adaic.org/resources/add_content/standards/12aarm/html/… for definition of those packages. - darkestkhan
@darkestkhan, I realise that, I consulted the ALRM before posting the question, but still the code will not compile. Why not create an answer using the above code as a starting point and I'll upvote and accept. :) - user5069935

1 Answers

5
votes

The problem is that, unlike Ada.Numerics.Pi, in which Pi is a component of Ada.Numerics, Discrete_Random is a child of Ada.Numerics.

Once you’ve said use Ada.Numerics, you could write just Pi in your program, but you have to actually with Ada.Numerics.Discrete_Random in order for it to be available.

In fact, you don’t need to with or use Ada.Numerics, this works just fine:

with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type Coin is (Heads, Tails);
   package Random_Coin is new Ada.Numerics.Discrete_Random(Coin);