1

I am writing with reference to my earlier post here:

Ada 2005 access type

In the first code diff.adb, there is the line:

procedure Solve is new Euler(Real, Vector, Ptr);

I don't understand how come the arguments of Euler area Real, Vector and Ptr because further down in the same code we have

Solve(Ident'Access, 1.0, 0.1, Answer);

with 4 arguments.

The original codes at the previous link are OK and are working.

2
  • 1
    It is generally advisable to use named notation when providing parameters for generics or for functions or procedures. Use of named notation clarifies what each parameter is used for and allows the order of the parameters to be changed.
    – Jim Rogers
    Mar 14 at 12:40
  • Yes indeed you're right. I got the example from Ben Ari Software for Ada engineers book. I'm learning both access types and generics at the same time here. I can try to modify the codes later to put in more meaningful variable names and use named notation. Mar 14 at 13:58

1 Answer 1

4

The first line is an example of Generic Instantiation. It creates an instance of the generic procedure Euler, passing the three concrete types required. The actual types supplied in the instantiation—Real, Vector and Ptr— match the Formal Types specified in euler.ads: Float_Type, Vector and Function_Ptr, in that order. Note that either positional or named association is permitted.

The second line is a Subprogram Call of the newly created procedure instance, which has four actual parameters: a Function_Ptr, two Real values and an output Vector. The new instance is named Solve, and it now has the same parameter profile as the procedure Euler.

What's the difference between a subprogram parameter (starting with keyword with) and a subprogram declaration?

In your example, Euler is not a formal subprogram declaration, which requires with; it is a generic subprogram specification, which does not. The examples may help illustrate the difference.

9
  • In Euler.ads that there are 4 parameters namely Function_Ptr, Init, H and Result. But Init and H are of the same type. So is this what you mean when you say Euler requires "3 concrete types"? My second point is the generic instantiation "new Euler" has the types Real. Vector and Ptr in order. But this order is not the positional association of what we have in Euler.ads in which we have Ptr, Float and Vector as arguments. So how does the matching between the instantiation and Euler in Euler.ads is made? But Solve has positional associations of parameters with Euler function in Euler.ads. Thanks Mar 14 at 4:40
  • If I instead write "procedure Solve is new Euler(Ptr, Vector, Real);" where the arguments order has been modified, then there is compilation error saying a Float_Type su expected. In the original procedure in my post "procedure Solve is new Euler(Real, Vector, Ptr);", why do we have Real, Vector and Ptr in that order? Which associations are they referring to? Thanks Mar 14 at 9:07
  • Procedure Solve I agree has the same number of arguments of procedure Euler (see Euler.ads in earlier post) and these arguments are in the same order (positional associations). But when it comes to instantiating the generic procedure Euler as the new procedure Solve, then I'm lost since now Solve takes only 3 arguments or types: Real, Vector and Ptr. What are Real, Vector and Ptr referring to? I'm trying to match the arguments with those of Euler but I am not able to do it. Mar 14 at 10:32
  • I can see the potential confusion; I've elaborated above.
    – trashgod
    Mar 14 at 11:21
  • You wrote: "The actual types supplied in the instantiation—Real, Vector and Ptr— match the Formal Types specified in euler.ads: Float_Type, Vector and Function_Ptr, in that order." Now I understand what you mean--in euler.ads, after the keyword "Generic", Type Float_Type, Type Vector and Type Function_Ptr are defined in that order. If I would change this order, the compiler will tell me. So positional association is used here. Mar 14 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.