comp.lang.ada
 help / color / mirror / Atom feed
* How to access Vector.Index_Type?
@ 2019-03-17 20:43 jakub.dabek
  2019-03-17 21:25 ` Simon Wright
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: jakub.dabek @ 2019-03-17 20:43 UTC (permalink / raw)


I have the following program 

    with Ada.Containers.Vectors;

    procedure Test is
        package MyVec is new Ada.Containers.Vectors(Natural, Natural);
        Var : MyVec.Index_Type;
    begin
        null;
    end;

I get the following error:  
"Index_Type" is not a visible entity of "MyVec"  

How can I access the index type (`Element_Type` gives the same error message), without just writing `Natural`?

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

* Re: How to access Vector.Index_Type?
  2019-03-17 20:43 How to access Vector.Index_Type? jakub.dabek
@ 2019-03-17 21:25 ` Simon Wright
  2019-03-18  0:12   ` jakub.dabek
  2019-03-18  4:17 ` gautier_niouzes
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Simon Wright @ 2019-03-17 21:25 UTC (permalink / raw)


jakub.dabek@gmail.com writes:

> I have the following program
>
>     with Ada.Containers.Vectors;
>
>     procedure Test is
>         package MyVec is new Ada.Containers.Vectors(Natural, Natural);
>         Var : MyVec.Index_Type;
>     begin
>         null;
>     end;
>
> I get the following error:
> "Index_Type" is not a visible entity of "MyVec"
>
> How can I access the index type (`Element_Type` gives the same error
> message), without just writing `Natural`?

You could use

   subtype My_Index_Type is MyVec.Extended_Index
     range MyVec.Extended_Index'First + 1 .. MyVec.Extended_Index'Last;


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

* Re: How to access Vector.Index_Type?
  2019-03-17 21:25 ` Simon Wright
@ 2019-03-18  0:12   ` jakub.dabek
  2019-03-18  8:21     ` Simon Wright
  0 siblings, 1 reply; 10+ messages in thread
From: jakub.dabek @ 2019-03-18  0:12 UTC (permalink / raw)


Simon Wright writes:
> You could use
> 
>    subtype My_Index_Type is MyVec.Extended_Index
>      range MyVec.Extended_Index'First + 1 .. MyVec.Extended_Index'Last;


This feels more like a "life-hack", requiring the knowledge of internal representation of `Extended_Index`. Is there really no better way to do that?



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

* Re: How to access Vector.Index_Type?
  2019-03-17 20:43 How to access Vector.Index_Type? jakub.dabek
  2019-03-17 21:25 ` Simon Wright
@ 2019-03-18  4:17 ` gautier_niouzes
  2019-03-18  5:27 ` J-P. Rosen
  2019-03-18 19:35 ` Jeffrey R. Carter
  3 siblings, 0 replies; 10+ messages in thread
From: gautier_niouzes @ 2019-03-18  4:17 UTC (permalink / raw)


>         package MyVec is new Ada.Containers.Vectors(Natural, Natural);
>         Var : MyVec.Index_Type;

Try:
  subtype My_Index is Natural;
  subtype My_Element is Natural;
  package MyVec is new Ada.Containers.Vectors(My_Index, My_Element);
  Var : My_Index; 


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

* Re: How to access Vector.Index_Type?
  2019-03-17 20:43 How to access Vector.Index_Type? jakub.dabek
  2019-03-17 21:25 ` Simon Wright
  2019-03-18  4:17 ` gautier_niouzes
@ 2019-03-18  5:27 ` J-P. Rosen
  2019-03-18  8:31   ` Dmitry A. Kazakov
  2019-03-18 19:35 ` Jeffrey R. Carter
  3 siblings, 1 reply; 10+ messages in thread
From: J-P. Rosen @ 2019-03-18  5:27 UTC (permalink / raw)


Le 17/03/2019 à 21:43, jakub.dabek@gmail.com a écrit :
> I have the following program
> 
>      with Ada.Containers.Vectors;
> 
>      procedure Test is
>          package MyVec is new Ada.Containers.Vectors(Natural, Natural);
>          Var : MyVec.Index_Type;
>      begin
>          null;
>      end;
> 
> I get the following error:
> "Index_Type" is not a visible entity of "MyVec"
> 
> How can I access the index type (`Element_Type` gives the same error message), without just writing `Natural`?
> 
Why would you do that? If you instantiate Vectors on Natural, it's 
because you want a vector of Natural! Therefore it makes sense to use 
Natural. The name "Index_Type" is a formal, not an actual type, it's 
just the package's business.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: How to access Vector.Index_Type?
  2019-03-18  0:12   ` jakub.dabek
@ 2019-03-18  8:21     ` Simon Wright
  0 siblings, 0 replies; 10+ messages in thread
From: Simon Wright @ 2019-03-18  8:21 UTC (permalink / raw)


jakub.dabek@gmail.com writes:

> Simon Wright writes:
>> You could use
>> 
>>    subtype My_Index_Type is MyVec.Extended_Index
>>      range MyVec.Extended_Index'First + 1 .. MyVec.Extended_Index'Last;
>
>
> This feels more like a "life-hack", requiring the knowledge of
> internal representation of `Extended_Index`. Is there really no better
> way to do that?

Well, as J-P Rosen has said, why would you ever need to do this in the
first place?

But, anyway, it's not an internal repreentation, it's public in ARM
A.18.2(7):
http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-2.html#p7


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

* Re: How to access Vector.Index_Type?
  2019-03-18  5:27 ` J-P. Rosen
@ 2019-03-18  8:31   ` Dmitry A. Kazakov
  2019-03-18 20:59     ` briot.emmanuel
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2019-03-18  8:31 UTC (permalink / raw)


On 2019-03-18 06:27, J-P. Rosen wrote:
> Le 17/03/2019 à 21:43, jakub.dabek@gmail.com a écrit :
>> I have the following program
>>
>>      with Ada.Containers.Vectors;
>>
>>      procedure Test is
>>          package MyVec is new Ada.Containers.Vectors(Natural, Natural);
>>          Var : MyVec.Index_Type;
>>      begin
>>          null;
>>      end;
>>
>> I get the following error:
>> "Index_Type" is not a visible entity of "MyVec"
>>
>> How can I access the index type (`Element_Type` gives the same error 
>> message), without just writing `Natural`?
>>
> Why would you do that? If you instantiate Vectors on Natural, it's 
> because you want a vector of Natural! Therefore it makes sense to use 
> Natural. The name "Index_Type" is a formal, not an actual type, it's 
> just the package's business.

surely for maintenance and code quality reasons. In the example 
everything is clear, but in other (more realistic) scenarios the package 
MyVec might come from other packages only to be used in Test. The 
procedure Test must then use the least possible assumption about the 
index type and be ready to the cases when the type would change, e.g. 
become an enumeration type.

P.S. It is a longstanding issue with visibility of formal generic 
parameters. Anybody who used generics was hit by it more than once. I 
don't know if anything was attempted recently to fix that. A life-hack 
(that does not apply to existing packages) was/is to rename or subtype 
each formal parameter of the generic:

    generic
       type T is <>;
       with package Bar ...
    package Foo is
       subtype The_T is T;
       package The_Bar renames Bar;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: How to access Vector.Index_Type?
  2019-03-17 20:43 How to access Vector.Index_Type? jakub.dabek
                   ` (2 preceding siblings ...)
  2019-03-18  5:27 ` J-P. Rosen
@ 2019-03-18 19:35 ` Jeffrey R. Carter
  3 siblings, 0 replies; 10+ messages in thread
From: Jeffrey R. Carter @ 2019-03-18 19:35 UTC (permalink / raw)


On 3/17/19 9:43 PM, jakub.dabek@gmail.com wrote:
> I have the following program
> 
>      with Ada.Containers.Vectors;
> 
>      procedure Test is
>          package MyVec is new Ada.Containers.Vectors(Natural, Natural);
>          Var : MyVec.Index_Type;
>      begin
>          null;
>      end;
> 
> I get the following error:
> "Index_Type" is not a visible entity of "MyVec"
> 
> How can I access the index type (`Element_Type` gives the same error message), without just writing `Natural`?

This is refreshing. Usually people complain that Ada makes them type too much. 
It's unusual to see someone complain that Ada makes them type less than they want.

If you are dealing with pkg Myvec, Ada presumes that you will look at the 
instantiation and see that it says

Index_Type => Natural

and use Natural when you want an index.

-- 
Jeff Carter
"What I wouldn't give for a large sock with horse manure in it."
Annie Hall
42

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

* Re: How to access Vector.Index_Type?
  2019-03-18  8:31   ` Dmitry A. Kazakov
@ 2019-03-18 20:59     ` briot.emmanuel
  2019-03-18 23:01       ` J-P. Rosen
  0 siblings, 1 reply; 10+ messages in thread
From: briot.emmanuel @ 2019-03-18 20:59 UTC (permalink / raw)


> P.S. It is a longstanding issue with visibility of formal generic 
> parameters. Anybody who used generics was hit by it more than once. I 
> don't know if anything was attempted recently to fix that. A life-hack 
> (that does not apply to existing packages) was/is to rename or subtype 
> each formal parameter of the generic:


I agree with this 100%.
This has been a huge issue to me when trying to work on "traits", and other use of generic packages that you want to combine with other packages. In some cases, you have access to Index_Type and sometimes not.
I like to remember this by thinking "if my code doesn't know how the package was instantiated, I can access the actual parameters, but if the instance is visible  can't.

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

* Re: How to access Vector.Index_Type?
  2019-03-18 20:59     ` briot.emmanuel
@ 2019-03-18 23:01       ` J-P. Rosen
  0 siblings, 0 replies; 10+ messages in thread
From: J-P. Rosen @ 2019-03-18 23:01 UTC (permalink / raw)


Le 18/03/2019 à 21:59, briot.emmanuel@gmail.com a écrit :
> I like to remember this by thinking "if my code doesn't know how the
> package was instantiated, I can access the actual parameters, but if
> the instance is visible  can't.

No, you can access the actual, it's the formal that you can't access

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

end of thread, other threads:[~2019-03-18 23:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-17 20:43 How to access Vector.Index_Type? jakub.dabek
2019-03-17 21:25 ` Simon Wright
2019-03-18  0:12   ` jakub.dabek
2019-03-18  8:21     ` Simon Wright
2019-03-18  4:17 ` gautier_niouzes
2019-03-18  5:27 ` J-P. Rosen
2019-03-18  8:31   ` Dmitry A. Kazakov
2019-03-18 20:59     ` briot.emmanuel
2019-03-18 23:01       ` J-P. Rosen
2019-03-18 19:35 ` Jeffrey R. Carter

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