comp.lang.ada
 help / color / mirror / Atom feed
* Heterogenous array
@ 2004-03-01  1:53 Björn Persson
  2004-03-01  2:22 ` Steve
  2004-03-01 16:48 ` Robert I. Eachus
  0 siblings, 2 replies; 11+ messages in thread
From: Björn Persson @ 2004-03-01  1:53 UTC (permalink / raw)


I need an array type that can hold elements of different types. My 
current attempt (somewhat simplified) is below. My question is, is there 
a way to do this without pointers?

    type T_Param_Type is (Int, Str);
    type Parameter_Definition(Param_Type : T_Param_Type) is record
       Name : A_Bounded_String_Type;
       case Param_Type is
          when Int =>
             Min : Integer;
             Max : Integer;
             Int_Value : Integer;
          when Str =>
             Str_Value : Ada.Strings.Unbounded.Unbounded_String;
       end case;
    end record;
    type Parameter_Definition_Pointer is access Parameter_Definition;
    type Parameter_Spec is array(Positive range <>) of
                           Parameter_Definition_Pointer;

"array(Positive range <>) of Parameter_Definition" gave me the error 
"unconstrained element type in array declaration". I also tried a tagged 
type with subtypes and "array(Positive range <>) of 
Parameter_Definition'Class", and got the same error.

This isn't really a dynamic data structure so I feel that pointers 
shouldn't be necessary. Any ideas?

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Heterogenous array
  2004-03-01  1:53 Heterogenous array Björn Persson
@ 2004-03-01  2:22 ` Steve
  2004-03-01 16:48 ` Robert I. Eachus
  1 sibling, 0 replies; 11+ messages in thread
From: Steve @ 2004-03-01  2:22 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1632 bytes --]

Change your type definition to have a default descriminant:

type Parameter_Definition(Param_Type:T_Param_Type:=Int) is record
...

You'll also find that to assign to one of the elements in the array, you
need to do a full record assignment to establish the type.  Once the type is
set you can manipulate individual field values if needed.

Steve
(The Duck)

"Bj�rn Persson" <spam-away@nowhere.nil> wrote in message
news:pIw0c.84623$dP1.233768@newsc.telia.net...
I need an array type that can hold elements of different types. My
current attempt (somewhat simplified) is below. My question is, is there
a way to do this without pointers?

    type T_Param_Type is (Int, Str);
    type Parameter_Definition(Param_Type : T_Param_Type) is record
       Name : A_Bounded_String_Type;
       case Param_Type is
          when Int =>
             Min : Integer;
             Max : Integer;
             Int_Value : Integer;
          when Str =>
             Str_Value : Ada.Strings.Unbounded.Unbounded_String;
       end case;
    end record;
    type Parameter_Definition_Pointer is access Parameter_Definition;
    type Parameter_Spec is array(Positive range <>) of
                           Parameter_Definition_Pointer;

"array(Positive range <>) of Parameter_Definition" gave me the error
"unconstrained element type in array declaration". I also tried a tagged
type with subtypes and "array(Positive range <>) of
Parameter_Definition'Class", and got the same error.

This isn't really a dynamic data structure so I feel that pointers
shouldn't be necessary. Any ideas?

-- 
Bj�rn Persson

jor ers @sv ge.
b n_p son eri nu





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

* Re: Heterogenous array
  2004-03-01  1:53 Heterogenous array Björn Persson
  2004-03-01  2:22 ` Steve
@ 2004-03-01 16:48 ` Robert I. Eachus
  2004-03-01 22:01   ` Björn Persson
  1 sibling, 1 reply; 11+ messages in thread
From: Robert I. Eachus @ 2004-03-01 16:48 UTC (permalink / raw)


Bj�rn Persson wrote:

> I need an array type that can hold elements of different types. My 
> current attempt (somewhat simplified) is below. My question is, is there 
> a way to do this without pointers?

Sure, try:

with Ada.Strings.Bounded; with Ada.Strings.Unbounded;
package Multirecord is

     package A_Bounded_String is new 
Ada.Strings.Bounded.Generic_Bounded_Length(80);
     subtype A_Bounded_String_Type is A_Bounded_String.Bounded_String;

     type T_Param_Type is (Int, Str);
     type Parameter_Definition(Param_Type : T_Param_Type := Int) is record
        Name : A_Bounded_String_Type;
        case Param_Type is
           when Int =>
              Min : Integer;
              Max : Integer;
              Int_Value : Integer;
           when Str =>
              Str_Value : Ada.Strings.Unbounded.Unbounded_String;
        end case;
     end record;

     type Parameter_Wrapper is record
       Content: Parameter_Definition;
     end record;

     type Parameter_Array is array(Positive range <>) of
                            Parameter_Wrapper;

end Multirecord;

It should compile without problems and do exactly what you want.  The 
type Parameter_Wrapper is needed so that the element type of 
Parameter_Array is constrained.  The rule that requires this may seem 
silly but it is not.  Objects of type Parameter_Definition can be 
different sizes, objects of type Parameter_Wrapper are not.  In effect, 
the reason for having the Parameter_Wrapper type is for the compiler to 
have a hook to do the mutable type thing. (Which in Ada is only a 
property of records with (defaulted) discriminants but never of array 
elements.)

Whether the implementation accomplishes this by making Parameter_Wrapper 
the size of the largest possible Parameter_Definition or uses "hidden" 
pointers is, of course, up to the implementation.  Some compilers alway 
"allocate the max," others only do that if the maximum size is below 
some threshold.  I am not sure whether there are any compilers that 
always use hidden pointers.

-- 
                                           Robert I. Eachus

"The only thing necessary for the triumph of evil is for good men to do 
nothing." --Edmund Burke




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

* Re: Heterogenous array
  2004-03-01 16:48 ` Robert I. Eachus
@ 2004-03-01 22:01   ` Björn Persson
  2004-03-01 23:29     ` Randy Brukardt
  2004-03-02  3:13     ` Steve
  0 siblings, 2 replies; 11+ messages in thread
From: Björn Persson @ 2004-03-01 22:01 UTC (permalink / raw)


Thank you, Steve and Robert! I'm afraid I still don't understand why 
Parameter_Wrapper would be necessary, since Steve's solution seems to 
work equally well - at least in Gnat 3.3.2. On the other hand I don't 
understand why a default value for the discriminant would make such a 
difference - but it does.

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Heterogenous array
  2004-03-01 22:01   ` Björn Persson
@ 2004-03-01 23:29     ` Randy Brukardt
  2004-03-02  2:56       ` Robert I. Eachus
  2004-03-02  3:13     ` Steve
  1 sibling, 1 reply; 11+ messages in thread
From: Randy Brukardt @ 2004-03-01 23:29 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 842 bytes --]

"Bj�rn Persson" <spam-away@nowhere.nil> wrote in message
news:JoO0c.84716$dP1.234457@newsc.telia.net...
> Thank you, Steve and Robert! I'm afraid I still don't understand why
> Parameter_Wrapper would be necessary, since Steve's solution seems to
> work equally well - at least in Gnat 3.3.2. On the other hand I don't
> understand why a default value for the discriminant would make such a
> difference - but it does.

It's not necessary in Ada 95 unless the components are aliased. I think
Robert uses too many aliased things, and thus has their restrictions on the
brain. :-)

(There is some hope that this restriction will be lifted in Ada 200Y,
because it doesn't really fix anything, and we're likely to adopt a
different solution to the problem that it was supposed to fix. But that's
TBD.)

                           Randy.








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

* Re: Heterogenous array
  2004-03-01 23:29     ` Randy Brukardt
@ 2004-03-02  2:56       ` Robert I. Eachus
  0 siblings, 0 replies; 11+ messages in thread
From: Robert I. Eachus @ 2004-03-02  2:56 UTC (permalink / raw)


Randy Brukardt wrote:

> It's not necessary in Ada 95 unless the components are aliased. I think
> Robert uses too many aliased things, and thus has their restrictions on the
> brain. :-)

Correct.  I wrote that while taking a break from working on a program 
that is fighting the aliased rules all over the place.  I actually had 
to change some type declarations to have an initial value, just so the 
subtypes statically matched.  I have no idea what that is supposed to 
accomplish, since arrays with different bounds can statically match if 
they take their bounds from the initial value.  (Yeah, I know, the 
actual subtypes are therefore both unbounded.  But I think we could have 
found a better rule.)

I didn't throw my computer out the window this morning. I took a break 
instead.  Guess why I am reading comp.lang.ada again...

-- 
                                           Robert I. Eachus

"The only thing necessary for the triumph of evil is for good men to do 
nothing." --Edmund Burke




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

* Re: Heterogenous array
  2004-03-01 22:01   ` Björn Persson
  2004-03-01 23:29     ` Randy Brukardt
@ 2004-03-02  3:13     ` Steve
  2004-03-02 22:29       ` Björn Persson
  1 sibling, 1 reply; 11+ messages in thread
From: Steve @ 2004-03-02  3:13 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1040 bytes --]

The way I look at it, the default descriminant is a way of telling the
compiler you may want to assign a record with a different discriminant value
to a record you have declared.  That way enough memory is allocated to hold
the largest variant.  If you don't give a default descriminant, that tells
the compiler you're going to explicitly select the variant for each record
you declare, so it can size each variant in the smallest allocation - with
the restriction that you cannot assign a different variant after the
declaration.

I hope this helps,
Steve
(The Duck)


"Bj�rn Persson" <spam-away@nowhere.nil> wrote in message
news:JoO0c.84716$dP1.234457@newsc.telia.net...
Thank you, Steve and Robert! I'm afraid I still don't understand why
Parameter_Wrapper would be necessary, since Steve's solution seems to
work equally well - at least in Gnat 3.3.2. On the other hand I don't
understand why a default value for the discriminant would make such a
difference - but it does.

-- 
Bj�rn Persson

jor ers @sv ge.
b n_p son eri nu





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

* Re: Heterogenous array
  2004-03-02  3:13     ` Steve
@ 2004-03-02 22:29       ` Björn Persson
  2004-03-03  9:43         ` Jean-Pierre Rosen
  0 siblings, 1 reply; 11+ messages in thread
From: Björn Persson @ 2004-03-02 22:29 UTC (permalink / raw)


Steve wrote:
> The way I look at it, the default descriminant is a way of telling the
> compiler you may want to assign a record with a different discriminant value
> to a record you have declared.  That way enough memory is allocated to hold
> the largest variant.  If you don't give a default descriminant, that tells
> the compiler you're going to explicitly select the variant for each record
> you declare, so it can size each variant in the smallest allocation - with
> the restriction that you cannot assign a different variant after the
> declaration.

Okay, after some experimentation I understand the difference, but I 
can't help thinking that it seems somewhat unlike Ada to say "make the 
discriminant immutable in all objects of this type" by not giving it a 
default value. Putting a "constant" somewhere would have been more 
intuitive.

Oh well, now I know how to do what I want. Thanks again!

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Heterogenous array
  2004-03-02 22:29       ` Björn Persson
@ 2004-03-03  9:43         ` Jean-Pierre Rosen
  2004-03-03 14:44           ` Björn Persson
  0 siblings, 1 reply; 11+ messages in thread
From: Jean-Pierre Rosen @ 2004-03-03  9:43 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1684 bytes --]


"Bj�rn Persson" <spam-away@nowhere.nil> a �crit dans le message de news:_U71c.51050$mU6.207186@newsb.telia.net...
>Steve wrote:
>> The way I look at it, the default descriminant is a way of telling the
>> compiler you may want to assign a record with a different discriminant value
>> to a record you have declared.  That way enough memory is allocated to hold
>> the largest variant.  If you don't give a default descriminant, that tells
>> the compiler you're going to explicitly select the variant for each record
>> you declare, so it can size each variant in the smallest allocation - with
>> the restriction that you cannot assign a different variant after the
>> declaration.
>
>Okay, after some experimentation I understand the difference, but I
>can't help thinking that it seems somewhat unlike Ada to say "make the
>discriminant immutable in all objects of this type" by not giving it a
>default value. Putting a "constant" somewhere would have been more
>intuitive.
Acutally, it's the other way round.

What you want to do is to be able to declare an unconstrained variable, i.e.:
V :  Parameter_Definition;

Since there is no discriminant constraint, the variable is not constrained to always have the same value of the discriminant.
However, a discriminant is used by the compiler to make address computations. It would be extremely dangerous to have an
uninitialized discriminant. Therefore, a declaration that does not specify discriminants is allowed only if there are default values
for the discriminants.

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





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

* Re: Heterogenous array
  2004-03-03  9:43         ` Jean-Pierre Rosen
@ 2004-03-03 14:44           ` Björn Persson
  2004-03-03 17:52             ` Jean-Pierre Rosen
  0 siblings, 1 reply; 11+ messages in thread
From: Björn Persson @ 2004-03-03 14:44 UTC (permalink / raw)


Jean-Pierre Rosen wrote:

> "Björn Persson" <spam-away@nowhere.nil> a écrit dans le message de news:_U71c.51050$mU6.207186@newsb.telia.net...
> 
>>Okay, after some experimentation I understand the difference, but I
>>can't help thinking that it seems somewhat unlike Ada to say "make the
>>discriminant immutable in all objects of this type" by not giving it a
>>default value. Putting a "constant" somewhere would have been more
>>intuitive.
> 
> Acutally, it's the other way round.
> 
> What you want to do is to be able to declare an unconstrained variable, i.e.:
> V :  Parameter_Definition;
> 
> Since there is no discriminant constraint, the variable is not constrained to always have the same value of the discriminant.
> However, a discriminant is used by the compiler to make address computations. It would be extremely dangerous to have an
> uninitialized discriminant. Therefore, a declaration that does not specify discriminants is allowed only if there are default values
> for the discriminants.

That's perfectly reasonable, but now consider this declaration:

    V : Parameter_Definition := (Str,
                                 To_Bounded_String("A"),
                                 To_Unbounded_String("x");

Here the discriminant is initialized, so with your reasoning it 
shouldn't matter whether there was a default value or not. Now let's try 
to assign another value to the variable:

    V := (Int, To_Bounded_String("B"), 1, 5, 2);

This assignment works if there was a default value for the discriminant. 
Otherwise it fails because the discriminant was locked to Str. There 
would be no reason for this if the sole purpose were to avoid 
uninitialized discriminants.

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Heterogenous array
  2004-03-03 14:44           ` Björn Persson
@ 2004-03-03 17:52             ` Jean-Pierre Rosen
  0 siblings, 0 replies; 11+ messages in thread
From: Jean-Pierre Rosen @ 2004-03-03 17:52 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1553 bytes --]


"Bj�rn Persson" <spam-away@nowhere.nil> a �crit dans le message de news:7bm1c.51092$mU6.207520@newsb.telia.net...
>Jean-Pierre Rosen wrote:
>> Since there is no discriminant constraint, the variable is not constrained to always have the same value of the discriminant.
>> However, a discriminant is used by the compiler to make address computations. It would be extremely dangerous to have an
>> uninitialized discriminant. Therefore, a declaration that does not specify discriminants is allowed only if there are default
values
>> for the discriminants.
>
>That's perfectly reasonable, but now consider this declaration:
>
>    V : Parameter_Definition := (Str,
>                                 To_Bounded_String("A"),
>                                 To_Unbounded_String("x");
>
>Here the discriminant is initialized, so with your reasoning it
>shouldn't matter whether there was a default value or not.
No, the important issue is how you declare the variable, initial values are irrelevant.
If you declare a variable with a discriminant constraint, it is constrained.
If you declare a variable without a discriminant constraint, it is unconstrained.

The only thing that could be allowed would be to declare unconstrained variables for types without default values for discriminants
only if they are explicitely initialized. I guess it was thought simpler to just require default values.

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





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

end of thread, other threads:[~2004-03-03 17:52 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-01  1:53 Heterogenous array Björn Persson
2004-03-01  2:22 ` Steve
2004-03-01 16:48 ` Robert I. Eachus
2004-03-01 22:01   ` Björn Persson
2004-03-01 23:29     ` Randy Brukardt
2004-03-02  2:56       ` Robert I. Eachus
2004-03-02  3:13     ` Steve
2004-03-02 22:29       ` Björn Persson
2004-03-03  9:43         ` Jean-Pierre Rosen
2004-03-03 14:44           ` Björn Persson
2004-03-03 17:52             ` Jean-Pierre Rosen

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