comp.lang.ada
 help / color / mirror / Atom feed
* Generating a random number, not sure why I'm getting this error
@ 2015-11-16  1:25 John Smith
  2015-11-16  2:29 ` Jeffrey R. Carter
  0 siblings, 1 reply; 4+ messages in thread
From: John Smith @ 2015-11-16  1:25 UTC (permalink / raw)


Hello,

I don't understand why this is failing to compile.  I don't understand the rationale for the failure above all else:

with Ada.Numerics.Discrete_Random;
...
Ada.Numerics.Discrete_Random.Reset(Ada.Numerics.Discrete_Random.Generator);

And this is the error that I get:
:13:15: invalid prefix in selected component "Ada.Numerics.Discrete_Random" -- refers to the first Ada.Numerics.Discrete_Random...

The Ada documentation says that Discrete_Random has a Reset function, so why the error?
https://www2.adacore.com/gap-static/GNAT_Book/html/aarm/AA-A-5-2.html

I think I'm grossly misunderstanding how I'm calling Ada functions inside of packages.


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

* Re: Generating a random number, not sure why I'm getting this error
  2015-11-16  1:25 Generating a random number, not sure why I'm getting this error John Smith
@ 2015-11-16  2:29 ` Jeffrey R. Carter
  2015-11-16  2:53   ` John Smith
  0 siblings, 1 reply; 4+ messages in thread
From: Jeffrey R. Carter @ 2015-11-16  2:29 UTC (permalink / raw)


On 11/15/2015 06:25 PM, John Smith wrote:
> 
> with Ada.Numerics.Discrete_Random; ... 
> Ada.Numerics.Discrete_Random.Reset(Ada.Numerics.Discrete_Random.Generator);

Generator is a type, not something you can pass to a subprogram. What would you
expect a call to Random to return?

> And this is the error that I get:
> 
> :13:15: invalid prefix in selected component "Ada.Numerics.Discrete_Random"
> -- refers to the first Ada.Numerics.Discrete_Random...

Not a great error msg.

Discrete_Random is not a package: it's a generic package. You can consider a
generic like a template for a package or subprogram. You create a package or
subprogram from the generic through what Ada calls instantiation. When you
instantiate Discrete_Random, you supply a discrete subtype to fill in for
Result_Subtype; Random returns a value of this subtype.

So, for example,

package Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Character;
-- Random is the name of the pkg created by the instantiation

Gen : Random.Generator;
C   : Character;

Random.Reset (Gen => Gen);
C := Random.Random (Gen);

-- 
Jeff Carter
"Apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, the fresh water system,
and public health, what have the Romans ever done for us?"
Monty Python's Life of Brian
80

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

* Re: Generating a random number, not sure why I'm getting this error
  2015-11-16  2:29 ` Jeffrey R. Carter
@ 2015-11-16  2:53   ` John Smith
  2015-11-16  8:51     ` Simon Wright
  0 siblings, 1 reply; 4+ messages in thread
From: John Smith @ 2015-11-16  2:53 UTC (permalink / raw)


On Sunday, November 15, 2015 at 9:29:06 PM UTC-5, Jeffrey R. Carter wrote:
> On 11/15/2015 06:25 PM, John Smith wrote:
> > 
> > with Ada.Numerics.Discrete_Random; ... 
> > Ada.Numerics.Discrete_Random.Reset(Ada.Numerics.Discrete_Random.Generator);
> 
> Generator is a type, not something you can pass to a subprogram. What would you
> expect a call to Random to return?
> 
> > And this is the error that I get:
> > 
> > :13:15: invalid prefix in selected component "Ada.Numerics.Discrete_Random"
> > -- refers to the first Ada.Numerics.Discrete_Random...
> 
> Not a great error msg.
> 
> Discrete_Random is not a package: it's a generic package. You can consider a
> generic like a template for a package or subprogram. You create a package or
> subprogram from the generic through what Ada calls instantiation. When you
> instantiate Discrete_Random, you supply a discrete subtype to fill in for
> Result_Subtype; Random returns a value of this subtype.
> 
> So, for example,
> 
> package Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Character;
> -- Random is the name of the pkg created by the instantiation
> 
> Gen : Random.Generator;
> C   : Character;
> 
> Random.Reset (Gen => Gen);
> C := Random.Random (Gen);
> 
> -- 
> Jeff Carter
> "Apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, the fresh water system,
> and public health, what have the Romans ever done for us?"
> Monty Python's Life of Brian
> 80

Ok, that's what I was missing.  I didn't realize that that was how generic packages should be used.  I figured that they should be used just like any other package.

Thank you.

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

* Re: Generating a random number, not sure why I'm getting this error
  2015-11-16  2:53   ` John Smith
@ 2015-11-16  8:51     ` Simon Wright
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2015-11-16  8:51 UTC (permalink / raw)


John Smith <yoursurrogategod@gmail.com> writes:

> I didn't realize that that was how generic packages should be used.  I
> figured that they should be used just like any other package.

See for example https://en.wikibooks.org/wiki/Ada_Programming/Generics -
also http://www.adaic.org/learn/materials/


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

end of thread, other threads:[~2015-11-16  8:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-16  1:25 Generating a random number, not sure why I'm getting this error John Smith
2015-11-16  2:29 ` Jeffrey R. Carter
2015-11-16  2:53   ` John Smith
2015-11-16  8:51     ` Simon Wright

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