comp.lang.ada
 help / color / mirror / Atom feed
From: Gilbert Gosseyn <hnptz@yahoo.de>
Subject: Re: generic with function procedure
Date: Wed, 10 Jun 2020 01:21:12 -0700 (PDT)
Date: 2020-06-10T01:21:12-07:00	[thread overview]
Message-ID: <3a31389d-d554-46f5-930e-495192b1fbcbo@googlegroups.com> (raw)
In-Reply-To: <2bbd5c7c-78da-4127-b85a-eeb3f9651456o@googlegroups.com>

On Tuesday, June 9, 2020 at 6:44:13 PM UTC+2, Shark8 wrote:
> On Friday, June 5, 2020 at 8:35:34 AM UTC-6, Gilbert Gosseyn wrote:
> > 
> > the message is: no visible subprogram matches the specification for "g"
> >                            no visible subprogram matches the specification for "h"
> 
> Well, first, let's look at some generic code:
>  Generic
>    Type Element is private;
>    Type Index   is (<>);
>    Type Vector  is Array( Index range <> ) of Element;
>    Zero : Element;
>    with Function "+"( Left, Right : Element ) return Element is <>;
>  Function Generic_Sum( Input : Vector ) return Element;
>  Function Generic_Sum( Input : Vector ) return Element is
>    Return Result : Element := Zero do
>      For Item of Input Loop
>        Result:= Result + Item;
>      End Loop;
>    End Return;
>  End Generic_Sum;
> 
> Now, if we say something like:
>   Package Example_1 is
>      Type X is private;
>      Type Y is range 1..7;
>      Type Vector is Array(Y range <>) of X;
> 
>      Function "+"( Right : Vector ) return Element;
>      Default : Constant X;
>   Private
>      Function Sum is new Generic_Sum(X,Y,Vector, Zero => Default, others => <>);
>      Type X is range 0..15; -- 4-bits.
>      Function "+"( Right : Vector ) return Element
>         renames Sum;
>      Default : Constant X := 0;
>   End Example_1;
> 
> ...doesn't work. Why?
> Well, for one, the point where you're trying to instantiate Generic_Sum into Sum, even though the parameter (X) matches the formal parameter, at this point in the code the only thing we know is that it's a private type... it doesn't have a "+" function that it can see, and so it will tell you that there's nothing to match with function "+".
> 
> Swapping the "function Sum is" and "type X is" lines would solve that problem, but there *might* be another, similar one: the constant (Default) hasn't yet been assigned a value and so the instantiation might fail. (I'd have to re-read the ARM,)
> 
> In your post the line "procedure nm is new denm(f=>f1,g=>g1,h=>h1);" is analogous to the situation above, albeit slightly different: you're explicitly specifying that subprograms "g1" and "h1" are to be respectively associated with parameters "g" and "h" -- this is all well and good, and the compiler has caught two mistakes for you:
> — no visible subprogram matches the specification for "g"
> — no visible subprogram matches the specification for "h" 
> 
> So it's telling you that of all the functions "g1" that it sees at the point of instantiation, none match the generic's specification of "function g(j : Integer; v : Real_Vector) return Real;"
> (and the same for "h".)
> 
> Now, at this point the compiler cannot do anything more: this is the realm where only the human programmer can tell what to do:
> — Is it a copy and paste-error creating the functions "g" and "h"? (Were they declared in the form of "f" which is "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"?)
> — Did you perhaps mean to make the formal parameters of denm, "g" and "h", to be "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"? (Perhaps being interrupted while refactoring it, and it's stuck in a form intermediate between what you had and what you want.)
> — Did you forget to make a "g1" and "h1"?

Apparently the problem is solved when I make the parameter list for f, g, and h the same. (it must be a well known fact when using more than one with. However, I missed it). Thanks to all contributors.

  reply	other threads:[~2020-06-10  8:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-05 14:35 generic with function procedure Gilbert Gosseyn
2020-06-05 14:52 ` gautier_niouzes
2020-06-05 15:45   ` Gilbert Gosseyn
2020-06-05 15:59     ` Dmitry A. Kazakov
2020-06-05 16:09       ` Anh Vo
2020-06-05 20:28     ` Jeffrey R. Carter
2020-06-09 16:44 ` Shark8
2020-06-10  8:21   ` Gilbert Gosseyn [this message]
2020-06-10  8:45     ` AdaMagica
2020-06-12 14:29     ` Shark8
2020-06-12 16:45     ` Simon Wright
2020-06-12 18:49       ` Simon Wright
replies disabled

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