comp.lang.ada
 help / color / mirror / Atom feed
* Generic type parameters
@ 1999-06-14  0:00 dommo1234
  1999-06-15  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 3+ messages in thread
From: dommo1234 @ 1999-06-14  0:00 UTC (permalink / raw)


This query regards generic packages, and their instantiation with some
actual type.

OK, I have a generic package that requires to be instantiated with a
number of different types. The generic has a number of primative methods
that need to return either the type that the generic package was
instantiated with, an access to the type, or a constant access to the
type, as in the example below :-

generic
    type TypeX is private;
package Example is
    function One(..) return TypeX;
    function Two(..) return TypeX_A;
    function Three(..) return TypeX_AC;
end Example;

... where 'TypeX_A' needs to be "access all TypeX"
    and   'TypeX_AC' needs to be "access constant TypeX".

The problem is that if I delcare the two types 'TypeX_A' and 'TypeX_AC'
inside the generic, then any code containing variables that catch the
values of functions 'Two' and 'Threee' inside my generic must be of type
<generic inst>.Type_A etc. This is not very readable or convenient!

The only (not so sensible) solution I can think of is provide the
generic with 3 formal parameters, ie. TypeX, TypeX_A and TypeX_AC ....

Does anyone have a neater solution to this??

Thanks,
Dom.



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Generic type parameters
  1999-06-15  0:00 ` Matthew Heaney
@ 1999-06-15  0:00   ` dommo1234
  0 siblings, 0 replies; 3+ messages in thread
From: dommo1234 @ 1999-06-15  0:00 UTC (permalink / raw)


In article <m37lp6cm5h.fsf@mheaney.ni.net>,
  Matthew Heaney <matthew_heaney@acm.org> wrote:
>
>
> I don't know about "neater," but you can create another generic
package
> in which to declare the access types, and import an instantiation of
> that package as a generic formal package:
>
> generic
>   type TypeX is private;
> package TypeX_Access_Types_G is
>
>   type TypeX_A is access all TypeX;
>
>   type TypeX_AC is access constant TypeX;
>
> end;
>
> with TypeX_Access_Types_G;
>
> generic
>
>   with package TypeX_Access_Types is
>     new TypeX_Access_Types_G (<>);
>
>   use TypeX_Access_Types;
>
> package Example is
>
>   <as above>
>
> end;
>

I think this is quite neat, but having done some experimentation, I''ve
hit another problem that seems to relate to accesibility levels, a
subject that I find very confusing. Here's my test program :-

generic
    type TypeX is private;
package TestGeneric is
    type TypeX_A is access all TypeX;
    type TypeX_AC is access constant TypeX;
end TestGeneric;

with TestGeneric;

generic
    type TypeX is private;
package OtherGeneric is
    package TypeXAccessTypes is new TestGeneric(TypeX);
    use TypeXAccessTypes;
    type TestType is
        record
            variable: aliased TypeX;
        end record;
    function returnNonConst(aa: access TestType) return TypeX_A;
end OtherGeneric;

package body OtherGeneric is
    function returnNonConst(aa: access TestType) return TypeX_A is
    begin
        -- The following causes a Program_Error at runtime
        -- unless an Unchecked_Access is passed in!!!
        return aa.variable'Access;
    end returnNonConst;
end OtherGeneric;

with OtherGeneric; package OthGenInst is new OtherGeneric(Integer);
with OthGenInst; use OthGenInst;
procedure Main is
    testVar2: aliased TestType;
begin
    returnNonConst(testVar2'Access).all := 6;            -- ERROR!
    returnNonConst(testVar2'Unchecked_Access).all := 6;  -- OK!
end Main;

Can someone explain exactly what accessibility levels for testVar2,
tsetVar2.variable etc. are in the various scopes so I can understand
the rules better ...

Thanks,
Dom.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Generic type parameters
  1999-06-14  0:00 Generic type parameters dommo1234
@ 1999-06-15  0:00 ` Matthew Heaney
  1999-06-15  0:00   ` dommo1234
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Heaney @ 1999-06-15  0:00 UTC (permalink / raw)


On 14 Jun 1999 16:25, dommo1234@my-deja.com wrote:

> generic
>     type TypeX is private;
> package Example is
>     function One(..) return TypeX;
>     function Two(..) return TypeX_A;
>     function Three(..) return TypeX_AC;
> end Example;
> 
> ... where 'TypeX_A' needs to be "access all TypeX"
>     and   'TypeX_AC' needs to be "access constant TypeX".
> 
> The problem is that if I delcare the two types 'TypeX_A' and 'TypeX_AC'
> inside the generic, then any code containing variables that catch the
> values of functions 'Two' and 'Threee' inside my generic must be of type
> <generic inst>.Type_A etc. This is not very readable or convenient!
> 
> The only (not so sensible) solution I can think of is provide the
> generic with 3 formal parameters, ie. TypeX, TypeX_A and TypeX_AC ....
> 
> Does anyone have a neater solution to this??


I don't know about "neater," but you can create another generic package
in which to declare the access types, and import an instantiation of
that package as a generic formal package:

generic
  type TypeX is private;
package TypeX_Access_Types_G is

  type TypeX_A is access all TypeX;

  type TypeX_AC is access constant TypeX;

end;


with TypeX_Access_Types_G;

generic

  with package TypeX_Access_Types is
    new TypeX_Access_Types_G (<>);

  use TypeX_Access_Types;

package Example is

  <as above>

end;





  




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

end of thread, other threads:[~1999-06-15  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-14  0:00 Generic type parameters dommo1234
1999-06-15  0:00 ` Matthew Heaney
1999-06-15  0:00   ` dommo1234

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