comp.lang.ada
 help / color / mirror / Atom feed
* variant records
@ 1986-10-31  9:08 Dean Luick
  1986-11-03 14:17 ` Bev Sobelman
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Dean Luick @ 1986-10-31  9:08 UTC (permalink / raw)


I am trying to write a stack package using a variant record in the main
record type and I am having great difficulty.  My problem is the following,
if I have the following definition:

type enum is (A, B, C, D);
type rec(kind : enum := A) is
    record
        case kind is
            when A | B =>       -- the actual values don't matter
                null;
            when C | D =>
                foo : integer;
    end record;

type node(xkind : enum := A);
type node_ptr is access node;
type node(xkind : enum := A) is
    record
        next : node_ptr;
        xrec : rec(xkind);
    end record;

stack       : node_ptr := NULL;
extra_nodes : node_ptr := NULL;

-- and I have the following later definiions:

procedure push(r : rec) is
    temp : node_ptr;
begin
    temp := new_node;
    temp.xrec := r;             -- position 'Z'
    temp.next := stack;
    stack := temp;
end push;

function new_node return node_ptr is
    temp : node_ptr;
begin
    temp := extra_nodes;
    if temp = NULL then
        temp := new node;
    else
        extra_nodes := extra_nodes.next;
    end if;
    return temp;
end new_node;

Suppose I create a rec of say, B, and try to push it.  At postion 'Z'
a constraint error is raised because of the different variant types ( in
the first case, temp's discriminant is A ).  Is there any way around this?
I was led to believe that I cound change the variant part of a type by
assigning it a whole aggregate (is that the right word?) say, like

case r.kind is 
    . . .
    when B =>
        temp.xrec := rec'(kind => B)
    . . .

Well, I tried that at position 'Z' and it failed.  I have run out of ideas.
Any help would be greatly appreciated.

dean

-- 

Dean Luick
uucp:	...!{allegra,harvard,ihnp4,seismo,topaz}!uwvax!dream!dean
arpa:	dean@dream.wisc.edu

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

* Re: variant records
  1986-10-31  9:08 variant records Dean Luick
@ 1986-11-03 14:17 ` Bev Sobelman
  1986-11-05 16:35 ` stuart
  1986-11-06 15:40 ` stt
  2 siblings, 0 replies; 20+ messages in thread
From: Bev Sobelman @ 1986-11-03 14:17 UTC (permalink / raw)



This may apply to your constraint error problem:

When you declare a rec variable, are you using the default discriminant 
and then reassigning the whole record, or are you overriding the
default when you declare the record variable?  Consider these:

      Rec_1 : rec (kind => B);     -- this is a constrained record

       --- VS ---

      Rec_2 : rec;                -- this is unconstrained with
          .                          default discrim. = A 
          .
          .
      Rec_2 := (kind => B);       -- now have discrim. = B, still
                                     unconstrained

I haven't looked at the whole thing carefully enough to know whether
this would make a difference at position 'Z', but I thought it was
worth mentioning.

Bev Sobelman
bhs@mitre-bedford.arpa

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

* Re: variant records
  1986-10-31  9:08 variant records Dean Luick
  1986-11-03 14:17 ` Bev Sobelman
@ 1986-11-05 16:35 ` stuart
  1986-11-06 15:40 ` stt
  2 siblings, 0 replies; 20+ messages in thread
From: stuart @ 1986-11-05 16:35 UTC (permalink / raw)



Objects created with an allocator are always constrained.  If 
explicit constraints are not supplied, the object is constrained 
by the default discriminant values (LRM 4.8(5)).  The easiest 
solution would be to declare the type "node" without discriminants:

    type node;
    type node_ptr is access node;
    type node is
        record
            next : node_ptr;
            xrec : rec;      -- will not be constrained
        end record;

If you want save space by allocating exact sized constrained objects,
"node" can be declared with discriminates.  However, calls on "new"
must include a constraint, and the free list would have to be an array 
of free lists, one for each possible discriminant value.

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

* Re: variant records
  1986-10-31  9:08 variant records Dean Luick
  1986-11-03 14:17 ` Bev Sobelman
  1986-11-05 16:35 ` stuart
@ 1986-11-06 15:40 ` stt
  2 siblings, 0 replies; 20+ messages in thread
From: stt @ 1986-11-06 15:40 UTC (permalink / raw)



You have fallen into a couple of Ada traps.
First, all objects created by an allocator are constrained,
even if the type has defaults (see LRM 4.8:5 "If the type
of the created object is an array type or a type with
discriminants, the the created object is always constrained.")
Hence, by saying simply "new node" you have created an object
with the default discriminant, and constrained to stay that way.

Secondly, by having a record component whose discriminant depends
on the discriminant of the enclosing record, the component is
constrained by the enclosing discriminant, and the enclosing
discriminant cannot be changed by assigning to just the component.

To solve your problem, you should either remove the discriminant
from the outer record type "node" and declare the xrec component
without a discriminant (hence unconstrained), or maintain separate
node free lists for each distinct discriminant value, and allocate
using "new node(discrim)" where discrim is the discriminant of
the component rec being pushed (this will require that you pass
discrim as a parameter to your "new_node" function).

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

* Variant Records
@ 1999-09-14  0:00 Shawn Barber
  1999-09-14  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Shawn Barber @ 1999-09-14  0:00 UTC (permalink / raw)


I have recently run into a small problem using variant 
records. I have been forced (against my better judgement) to 
use a data structure consisting of a variant record, and in 
this variant record a field of a particular variant is 
another variant record. I already know the pain which has 
been inflicted by using such a data structure. My question 
is when I do a compare, say Var_1 = Var_2 or an assignment 
are the fields compaired/assigned field by field or all at 
once or in some other manor? Thanks.

Shawn

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!





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

* Re: Variant Records
  1999-09-14  0:00 Variant Records Shawn Barber
  1999-09-14  0:00 ` Matthew Heaney
  1999-09-14  0:00 ` Mark Lundquist
@ 1999-09-14  0:00 ` David C. Hoos, Sr.
  2 siblings, 0 replies; 20+ messages in thread
From: David C. Hoos, Sr. @ 1999-09-14  0:00 UTC (permalink / raw)



Shawn Barber <SbarberNOksSPAM@snet.net> wrote in message
news:202784de.3f456337@usw-ex0106-042.remarq.com...
> I have recently run into a small problem using variant
> records. I have been forced (against my better judgement) to
> use a data structure consisting of a variant record, and in
> this variant record a field of a particular variant is
> another variant record. I already know the pain which has
> been inflicted by using such a data structure. My question
> is when I do a compare, say Var_1 = Var_2 or an assignment
> are the fields compaired/assigned field by field or all at
> once or in some other manor? Thanks.
>
This is impossible to answer precisely without knowing what you mean
by "all at once."  Unless the record is very small (which wouldn't
seem to be the case, from your description), it almost certainly
is _not_ "all at once", in that several loads/stores will be
needed to accomplish assignment or comparison.

Perhaps if you explained more the nature of the "problem" you're
having, as well as the platform, compiler, and other details,
you'd be more likely to get meaningful help.










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

* Re: Variant Records
  1999-09-14  0:00 Variant Records Shawn Barber
@ 1999-09-14  0:00 ` Matthew Heaney
  1999-09-14  0:00 ` Mark Lundquist
  1999-09-14  0:00 ` David C. Hoos, Sr.
  2 siblings, 0 replies; 20+ messages in thread
From: Matthew Heaney @ 1999-09-14  0:00 UTC (permalink / raw)


In article <202784de.3f456337@usw-ex0106-042.remarq.com> , Shawn Barber 
<SbarberNOksSPAM@snet.net>  wrote:

> I have recently run into a small problem using variant records. I have been
> forced (against my better judgement) to use a data structure consisting of a
> variant record,

What do you have against variant records?


> and in this variant record a field of a particular variant is another variant
> record.

What's wrong with that?


> I already know the pain which has been inflicted by using such a data
> structure.

What kind of pain?  Depending on the nature of your problem, that might be
exactly the data structure you need.


> My question is when I do a compare, say Var_1 = Var_2 or an assignment are the
> fields compaired/assigned field by field or all at once or in some other
> manner? Thanks.

Both comparison and assignment are done field-by-field, although this isn't
something you'll find in the RM.  How else would you expect it to be done?
And why do you need to know?

Give us some specific details about what you think the problem is.





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

* Re: Variant Records
  1999-09-14  0:00 Variant Records Shawn Barber
  1999-09-14  0:00 ` Matthew Heaney
@ 1999-09-14  0:00 ` Mark Lundquist
  1999-09-14  0:00 ` David C. Hoos, Sr.
  2 siblings, 0 replies; 20+ messages in thread
From: Mark Lundquist @ 1999-09-14  0:00 UTC (permalink / raw)


Shawn Barber wrote:

> I have recently run into a small problem using variant
> records. I have been forced (against my better judgement) to
> use a data structure consisting of a variant record, and in
> this variant record a field of a particular variant is
> another variant record. I already know the pain which has
> been inflicted by using such a data structure. My question
> is when I do a compare, say Var_1 = Var_2 or an assignment
> are the fields compaired/assigned field by field or all at
> once or in some other manor? Thanks.

You mean, if you examined the generated code would you be able to identify
discrete comparisons of each field, as opposed to, say, a big block-compare?
Is that the idea?

If so, the answer is that it depends on your implementation and on exactly what
the type declaration looks like.

Did you know that you can use constrained subtypes to catch (in some cases, at
compile time) places where the discriminant values aren't right (don't make
sense, etc.)?  I don't know if this helps you or not -- just a hunch...

Why do you care how equality is tested?






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

* Variant records..
@ 2004-02-15 22:51 Ganesh Ramasivan
  2004-02-16 10:57 ` Preben Randhol
  2004-02-16 23:17 ` tmoran
  0 siblings, 2 replies; 20+ messages in thread
From: Ganesh Ramasivan @ 2004-02-15 22:51 UTC (permalink / raw)


In the following example, how would i forbid the user of data type Foo
from specifying the following:

John : Foo(Vehicle => MAN, Power => AUTOMATIC);

ie. is there a way to restrict the user from specifying certain types
as variants?

Thanks for your input.

Ganesh

Example:

procedure Variant is

   type Vehicle_Type is (CAR, LEGS, BIKE, PLANE);
   type Vehicle_Power is (MANUAL, AUTOMATIC);

   type Foo(Vehicle : Vehicle_Type; Power: Vehicle_Power) is
    record
        case Vehicle is
	     when CAR  =>
            null;
	     when LEGS =>
            null;
	     when BIKE =>
            null;
	     when PLANE =>
            null;
         when others =>
            null;
        end case;
    end record;

    Audi : Foo(Vehicle => CAR, Power => MANUAL); 
begin
 null;
end Variant;



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

* Re: Variant records..
  2004-02-15 22:51 Variant records Ganesh Ramasivan
@ 2004-02-16 10:57 ` Preben Randhol
  2004-02-16 15:25   ` Ganesh Ramasivan
  2004-02-16 16:59   ` Ganesh Ramasivan
  2004-02-16 23:17 ` tmoran
  1 sibling, 2 replies; 20+ messages in thread
From: Preben Randhol @ 2004-02-16 10:57 UTC (permalink / raw)


On 2004-02-15, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> In the following example, how would i forbid the user of data type Foo
> from specifying the following:
>
> John : Foo(Vehicle => MAN, Power => AUTOMATIC);
>
> ie. is there a way to restrict the user from specifying certain types
> as variants?

I guess this is homework? If it is why don't you simply try to do this
and see what happens?


-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



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

* Re: Variant records..
  2004-02-16 10:57 ` Preben Randhol
@ 2004-02-16 15:25   ` Ganesh Ramasivan
  2004-02-16 15:46     ` Ludovic Brenta
  2004-02-16 16:16     ` Preben Randhol
  2004-02-16 16:59   ` Ganesh Ramasivan
  1 sibling, 2 replies; 20+ messages in thread
From: Ganesh Ramasivan @ 2004-02-16 15:25 UTC (permalink / raw)


Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote in message news:<slrnc318ga.3ja.randhol+valid_for_reply_from_news@k-083152.nt.ntnu.no>...
> On 2004-02-15, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> > In the following example, how would i forbid the user of data type Foo
> > from specifying the following:
> >
> > John : Foo(Vehicle => MAN, Power => AUTOMATIC);
> >
> > ie. is there a way to restrict the user from specifying certain types
> > as variants?
> 
> I guess this is homework? If it is why don't you simply try to do this
> and see what happens?

This is just a simple example I am working with as I am an ada newbie.

I tried and it compiles. What I am trying to do is to prevent the user
from creating types that do not make sense.

Ganesh



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

* Re: Variant records..
  2004-02-16 15:25   ` Ganesh Ramasivan
@ 2004-02-16 15:46     ` Ludovic Brenta
  2004-02-16 16:16     ` Preben Randhol
  1 sibling, 0 replies; 20+ messages in thread
From: Ludovic Brenta @ 2004-02-16 15:46 UTC (permalink / raw)


ganesh.ramasivan@gdcanada.com (Ganesh Ramasivan) writes:

> Preben Randhol wrote...
> > On 2004-02-15, Ganesh Ramasivan wrote:
> > > In the following example, how would i forbid the user of data type Foo
> > > from specifying the following:
> > >
> > > John : Foo(Vehicle => MAN, Power => AUTOMATIC);
> > >
> > > ie. is there a way to restrict the user from specifying certain types
> > > as variants?
> > 
> > I guess this is homework? If it is why don't you simply try to do this
> > and see what happens?
> 
> This is just a simple example I am working with as I am an ada newbie.
> 
> I tried and it compiles. What I am trying to do is to prevent the user
> from creating types that do not make sense.

IOW, you want to introduce a constraint between Vehicle and Power.
Personally, I would replace the two discriminants with just one:

type Vehicle is (Man, Bicycle, Car_Automatic, Car_Manual, Helicopter, Jet,
                 Space_Launcher);

The alternative would be to make type Foo private, hide the
discriminants from the user, and provide a constructor function.  This
however replaces the static checks performed by the compiler with a
run-time check, so your user needs a unit test wherever they call
New_Foo.

procedure Vehicles is

   package P is
      type Vehicle_Type is (Man, Bicycle, Car, Helicopter, Jet,
                            Space_Launcher);
      type Power_Type is (Automatic, Manual, None);
      type Foo (<>) is private;
      function New_Foo (Vehicle : Vehicle_Type; Power : Power_Type) return Foo;
   private
      type Foo (Vehicle : Vehicle_Type; Power : Power_Type) is record
         Horsepower : Float;
         case Power is
            when Automatic | Manual =>
               Gears : Positive;
            when others =>
               null;
         end case;
      end record;
   end P;

   package body P is
      function New_Foo (Vehicle : Vehicle_Type; Power : Power_Type) return Foo
      is
      begin
         case Vehicle is
            when Car =>
               if Power = None then raise Constraint_Error; end if;
            when others =>
               if Power /= None then raise Constraint_Error; end if;
         end case;
         declare
            Result : Foo (Vehicle, Power);
         begin
            return Result;
         end;
      end New_Foo;
   end P;

   use P;

   F : Foo := New_Foo (Vehicle => Man, Power => Automatic);
begin
   null;
end Vehicles;

-- 
Ludovic Brenta.



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

* Re: Variant records..
  2004-02-16 15:25   ` Ganesh Ramasivan
  2004-02-16 15:46     ` Ludovic Brenta
@ 2004-02-16 16:16     ` Preben Randhol
  2004-02-17 14:37       ` Ganesh Ramasivan
  1 sibling, 1 reply; 20+ messages in thread
From: Preben Randhol @ 2004-02-16 16:16 UTC (permalink / raw)


On 2004-02-16, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote in message news:<slrnc318ga.3ja.randhol+valid_for_reply_from_news@k-083152.nt.ntnu.no>...
>> On 2004-02-15, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
>> > In the following example, how would i forbid the user of data type Foo
>> > from specifying the following:
>> >
>> > John : Foo(Vehicle => MAN, Power => AUTOMATIC);
>> >
>> > ie. is there a way to restrict the user from specifying certain types
>> > as variants?
>> 
>> I guess this is homework? If it is why don't you simply try to do this
>> and see what happens?
>
> This is just a simple example I am working with as I am an ada newbie.
>
> I tried and it compiles. What I am trying to do is to prevent the user
> from creating types that do not make sense.

If you put

   Audio : Foo(Vehicle => MAN, Power => AUTOMATIC);

it won't compile, so I do not really understand your question.

-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



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

* Re: Variant records..
  2004-02-16 10:57 ` Preben Randhol
  2004-02-16 15:25   ` Ganesh Ramasivan
@ 2004-02-16 16:59   ` Ganesh Ramasivan
  2004-02-16 20:14     ` Simon Wright
  1 sibling, 1 reply; 20+ messages in thread
From: Ganesh Ramasivan @ 2004-02-16 16:59 UTC (permalink / raw)


Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote in message news:<slrnc318ga.3ja.randhol+valid_for_reply_from_news@k-083152.nt.ntnu.no>...
> On 2004-02-15, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> > In the following example, how would i forbid the user of data type Foo
> > from specifying the following:
> >
> > John : Foo(Vehicle => MAN, Power => AUTOMATIC);
> >
> > ie. is there a way to restrict the user from specifying certain types
> > as variants?
> 
> I guess this is homework? If it is why don't you simply try to do this
> and see what happens?

Okay.. I should probably have been a bit more realistic.. I am new to
ada but this is no homework assignment.

type RADAR_TYPE_CHOICE is (RADAR_A, RADAR_B, RADAR_C);
    
type RADAR_A_COMMAND_CHOICE is (RADAR_A_STATE,
                                RADAR_A_MODE,
                                RADAR_A_DISPLAY_RANGE_SELECT);
                                      
type RADAR_B_COMMAND_CHOICE is (RADAR_B_STATE,
                                RADAR_B_MODE,
                                RADAR_B_DISPLAY_RANGE_SELECT);

type RADAR_C_COMMAND_CHOICE is (RADAR_C_STATE,
                                RADAR_C_MODE,
                                RADAR_C_DISPLAY_RANGE_SELECT);

type RADAR_CHOICE(RADAR_TYPE : RADAR_TYPE_CHOICE :=
RADAR_TYPE_CHOICE'FIRST) is
      record   
        case RADAR_TYPE is
    	  when  RADAR_A =>
            null;
    	  when  RADAR_B  =>
            null;
    	  when  RADAR_C =>
            null;            
        end case;
      end record;

How would I specify a variant record which will only allow me to
select RADAR_A for the RADAR_TYPE_CHOICE and RADAR_A_STATE for the
command choice but not RADAR_C_STATE?



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

* Re: Variant records..
  2004-02-16 16:59   ` Ganesh Ramasivan
@ 2004-02-16 20:14     ` Simon Wright
  2004-02-17 14:56       ` Ganesh Ramasivan
  0 siblings, 1 reply; 20+ messages in thread
From: Simon Wright @ 2004-02-16 20:14 UTC (permalink / raw)


ganesh.ramasivan@gdcanada.com (Ganesh Ramasivan) writes:

> type RADAR_TYPE_CHOICE is (RADAR_A, RADAR_B, RADAR_C);
>     
> type RADAR_A_COMMAND_CHOICE is (RADAR_A_STATE,
>                                 RADAR_A_MODE,
>                                 RADAR_A_DISPLAY_RANGE_SELECT);
>                                       
> type RADAR_B_COMMAND_CHOICE is (RADAR_B_STATE,
>                                 RADAR_B_MODE,
>                                 RADAR_B_DISPLAY_RANGE_SELECT);
> 
> type RADAR_C_COMMAND_CHOICE is (RADAR_C_STATE,
>                                 RADAR_C_MODE,
>                                 RADAR_C_DISPLAY_RANGE_SELECT);

I would say you seem to have a basic concept Radar with a set of
Commands that might apply to any Radar, and three instances of Radar.

   type Radar_Kind is (Radar_A, Radar_B, Radar_C);
   type Radar_Command is (State, Mode, Display_Range_Select);

I can see that you might have different sets of states for Radar_A
than those for Radar_B, but so far your code doesn't say that.

> type RADAR_CHOICE(RADAR_TYPE : RADAR_TYPE_CHOICE :=
> RADAR_TYPE_CHOICE'FIRST) is
>       record   
>         case RADAR_TYPE is
>     	  when  RADAR_A =>
>             null;
>     	  when  RADAR_B  =>
>             null;
>     	  when  RADAR_C =>
>             null;            
>         end case;
>       end record;
> 
> How would I specify a variant record which will only allow me to
> select RADAR_A for the RADAR_TYPE_CHOICE and RADAR_A_STATE for the
> command choice but not RADAR_C_STATE?

I don't see any command choice here?

I don't really grasp why you want this Radar_Choice thing, perhaps it
would help if you could say what you would like to appear in the
places you've marked 'null' ... is it

type RADAR_CHOICE
  (RADAR_TYPE : RADAR_TYPE_CHOICE := RADAR_TYPE_CHOICE'FIRST) is
      record   
        case RADAR_TYPE is
    	  when  RADAR_A =>
            Radar_A_Command : Radar_A_Command_Choice;
    	  when  RADAR_B  =>
            Radar_B_Command : Radar_B_Command_Choice;
    	  when  RADAR_C =>
            Radar_C_Command : Radar_C_Command_Choice;
        end case;
      end record;



Sorry that I'm querying your design intent rather than your actual
question, I haven't understood the question properly!

-- 
Simon Wright                               100% Ada, no bugs.



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

* Re: Variant records..
  2004-02-15 22:51 Variant records Ganesh Ramasivan
  2004-02-16 10:57 ` Preben Randhol
@ 2004-02-16 23:17 ` tmoran
  1 sibling, 0 replies; 20+ messages in thread
From: tmoran @ 2004-02-16 23:17 UTC (permalink / raw)


How about instead:
   type Vehicle_Type is abstract tagged null record;

   procedure Set_Speed(This : in out Vehicle_Type; S : in Speeds);
   function State_Of(This : Vehicle_Type) return States;

   type Vehicle_Power is (MANUAL, AUTOMATIC);

   type Car is new Vehicle_Type(Power : Vehicle_Power) with null record;
   procedure Shift_Gears(This : in out Car;
                         New_Gear : Positive);

   type Man is new Vehicle_Type with null record;
   ...

   Audi : CAR(Power => MANUAL);
   John : Man;
   ...
   Set_Speed(Audi, 65);
   Set_Speed(John, 6);
   Shift_Gears(Audi, New_Gear=>2);
   Shift_Gears(John, New_Gear=>2);  -- illegal



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

* Re: Variant records..
  2004-02-16 16:16     ` Preben Randhol
@ 2004-02-17 14:37       ` Ganesh Ramasivan
  2004-02-17 15:43         ` Preben Randhol
  0 siblings, 1 reply; 20+ messages in thread
From: Ganesh Ramasivan @ 2004-02-17 14:37 UTC (permalink / raw)


Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote in message news:<slrnc31r7c.r6.randhol+valid_for_reply_from_news@k-083152.nt.ntnu.no>...
> On 2004-02-16, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> > Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote in message news:<slrnc318ga.3ja.randhol+valid_for_reply_from_news@k-083152.nt.ntnu.no>...
> >> On 2004-02-15, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> >> > In the following example, how would i forbid the user of data type Foo
> >> > from specifying the following:
> >> >
> >> > John : Foo(Vehicle => MAN, Power => AUTOMATIC);
> >> >
> >> > ie. is there a way to restrict the user from specifying certain types
> >> > as variants?
> >> 
> >> I guess this is homework? If it is why don't you simply try to do this
> >> and see what happens?
> >
> > This is just a simple example I am working with as I am an ada newbie.
> >
> > I tried and it compiles. What I am trying to do is to prevent the user
> > from creating types that do not make sense.
> 
> If you put
> 
>    Audio : Foo(Vehicle => MAN, Power => AUTOMATIC);
> 
> it won't compile, so I do not really understand your question.

Sorry, but I don't see why you think this won't compile. I tried it
and it does compile.



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

* Re: Variant records..
  2004-02-16 20:14     ` Simon Wright
@ 2004-02-17 14:56       ` Ganesh Ramasivan
  2004-02-17 15:31         ` Robert I. Eachus
  0 siblings, 1 reply; 20+ messages in thread
From: Ganesh Ramasivan @ 2004-02-17 14:56 UTC (permalink / raw)


Simon Wright <simon@pushface.org> wrote in message news:<x7vwu6mwzm5.fsf@smaug.pushface.org>...
> ganesh.ramasivan@gdcanada.com (Ganesh Ramasivan) writes:
> 
> > type RADAR_TYPE_CHOICE is (RADAR_A, RADAR_B, RADAR_C);
> >     
> > type RADAR_A_COMMAND_CHOICE is (RADAR_A_STATE,
> >                                 RADAR_A_MODE,
> >                                 RADAR_A_DISPLAY_RANGE_SELECT);
> >                                       
> > type RADAR_B_COMMAND_CHOICE is (RADAR_B_STATE,
> >                                 RADAR_B_MODE,
> >                                 RADAR_B_DISPLAY_RANGE_SELECT);
> > 
> > type RADAR_C_COMMAND_CHOICE is (RADAR_C_STATE,
> >                                 RADAR_C_MODE,
> >                                 RADAR_C_DISPLAY_RANGE_SELECT);
> 
> I would say you seem to have a basic concept Radar with a set of
> Commands that might apply to any Radar, and three instances of Radar.
> 
>    type Radar_Kind is (Radar_A, Radar_B, Radar_C);
>    type Radar_Command is (State, Mode, Display_Range_Select);
> 
> I can see that you might have different sets of states for Radar_A
> than those for Radar_B, but so far your code doesn't say that.
> 
> > type RADAR_CHOICE(RADAR_TYPE : RADAR_TYPE_CHOICE :=
> > RADAR_TYPE_CHOICE'FIRST) is
> >       record   
> >         case RADAR_TYPE is
> >     	  when  RADAR_A =>
> >             null;
> >     	  when  RADAR_B  =>
> >             null;
> >     	  when  RADAR_C =>
> >             null;            
> >         end case;
> >       end record;
> > 
> > How would I specify a variant record which will only allow me to
> > select RADAR_A for the RADAR_TYPE_CHOICE and RADAR_A_STATE for the
> > command choice but not RADAR_C_STATE?
> 
> I don't see any command choice here?
> 
> I don't really grasp why you want this Radar_Choice thing, perhaps it
> would help if you could say what you would like to appear in the
> places you've marked 'null' ... is it
> 
> type RADAR_CHOICE
>   (RADAR_TYPE : RADAR_TYPE_CHOICE := RADAR_TYPE_CHOICE'FIRST) is
>       record   
>         case RADAR_TYPE is
>     	  when  RADAR_A =>
>             Radar_A_Command : Radar_A_Command_Choice;
>     	  when  RADAR_B  =>
>             Radar_B_Command : Radar_B_Command_Choice;
>     	  when  RADAR_C =>
>             Radar_C_Command : Radar_C_Command_Choice;
>         end case;
>       end record;
> 
> 
> 
> Sorry that I'm querying your design intent rather than your actual
> question, I haven't understood the question properly!

Sorry. I should not have put in null there. 

type RADAR_TYPE_CHOICE is (RADAR_A, RADAR_B);
     
type RADAR_A_COMMAND_CHOICE is (RADAR_A_STATE,
                                RADAR_A_MODE,
                                RADAR_A_DISPLAY_RANGE_SELECT);
                                      
type RADAR_B_COMMAND_CHOICE is (RADAR_B_STATE,
                                RADAR_B_MODE,
                                RADAR_B_DISPLAY_RANGE_SELECT);

type RADAR_A_COMMAND_TYPE
 (RADAR_COMMAND : RADAR_A_COMMAND_CHOICE :=
RADAR_A_COMMAND_CHOICE'FIRST) is
      record
          case RADAR_COMMAND is
	        when RADAR_A_DISPLAY_RANGE_SELECT =>
	            < a whole bunch of operations...>
            when others =>
                null;
          end case;
      end record;

type RADAR_B_COMMAND_TYPE
 (RADAR_COMMAND : RADAR_B_COMMAND_CHOICE :=
RADAR_B_COMMAND_CHOICE'FIRST) is
      record
          case RADAR_COMMAND is
	        when RADAR_B_DISPLAY_RANGE_SELECT =>
	            < a whole bunch of operations...>
            when others =>
                null;
          end case;
      end record;

type RADAR_CHOICE
   (RADAR_TYPE : RADAR_TYPE_CHOICE := RADAR_TYPE_CHOICE'FIRST) is
       record   
         case RADAR_TYPE is
     	  when  RADAR_A =>
             RADAR_A_COMMAND : RADAR_A_COMMAND_CHOICE(????)      	  
when  RADAR_B =>
             RADAR_B_COMMAND : RADAR_B_COMMAND_CHOICE(????)
         end case;
       end record;


Anyways, in the case above, how can I pass a second discriminant. This
is because I need to be able to select a particular command for a
particular radar.

Sorry for not having said this in the first place.

Ganesh



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

* Re: Variant records..
  2004-02-17 14:56       ` Ganesh Ramasivan
@ 2004-02-17 15:31         ` Robert I. Eachus
  0 siblings, 0 replies; 20+ messages in thread
From: Robert I. Eachus @ 2004-02-17 15:31 UTC (permalink / raw)


Ganesh Ramasivan wrote:

> Anyways, in the case above, how can I pass a second discriminant. This
> is because I need to be able to select a particular command for a
> particular radar.

One way is:

  type RADAR_TYPE_CHOICE is (RADAR_A, RADAR_B);

  type RADAR_COMMAND_CHOICE is (RADAR_STATE,
                                RADAR_MODE,
                                RADAR_DISPLAY_RANGE_SELECT);

  type RADAR_COMMAND_TYPE
       (RADAR_TYPE_CHOICE := RADAR_A;
        RADAR_COMMAND_CHOICE := RADAR_STATE)is
     record
       case RADAR_COMMAND_CHOICE is
       when RADAR_DISPLAY_RANGE_SELECT =>
         < a whole bunch of parameters...>
         case RADAR_TYPE_CHOICE is
         when RADAR_A =>
           <Radar A specific parameters>
         when RADAR_B =>
           <Radar B specific parameters>
         end case;
       when others =>
         null;
       end case;
     end record;

But with this approach, you can't have indentically named elements that 
are of different types or subtypes for Radars A and B.  You can of 
course give those parameters different names.

Or you can have:

     type Radar_A_Display_Range_Select is...
     type Radar_B_Display_Range_Select is...

type RADAR_COMMAND_TYPE
       (RADAR_TYPE_CHOICE := RADAR_A;
        RADAR_COMMAND_CHOICE := RADAR_STATE)is
     record
       case RADAR_COMMAND_CHOICE is
       when RADAR_DISPLAY_RANGE_SELECT =>
         case RADAR_TYPE_CHOICE is
         when RADAR_A =>
              RADAR_A_DISPLAY_RANGE: RADAR_A_DISPLAY_RANGE_SELECT;
         when RADAR_B =>
              RADAR_B_DISPLAY_RANGE: RADAR_B_DISPLAY_RANGE_SELECT;
         end case;
       when others =>
         null;
       end case;
     end record;

The potential probleme with this approach is the potentially long names 
for the individual elements.  However, if you set the command via an 
aggregate, that problem goes away.  (Unless your software development 
plan requires named notation for aggregates. ;-)

Either style works.  Does this answer your question?

-- 
                                           Robert I. Eachus

"The war on terror is a different kind of war, waged capture by capture, 
cell by cell, and victory by victory. Our security is assured by our 
perseverance and by our sure belief in the success of liberty." -- 
George W. Bush




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

* Re: Variant records..
  2004-02-17 14:37       ` Ganesh Ramasivan
@ 2004-02-17 15:43         ` Preben Randhol
  0 siblings, 0 replies; 20+ messages in thread
From: Preben Randhol @ 2004-02-17 15:43 UTC (permalink / raw)


On 2004-02-17, Ganesh Ramasivan <ganesh.ramasivan@gdcanada.com> wrote:
> Sorry, but I don't see why you think this won't compile. I tried it
> and it does compile.

Which compiler are you using?

Compiling the program (see below for the exact source code) I get:

% gnatmake variant.adb
gnatgcc -c variant.adb
variant.adb:22:27: "MAN" is undefined
gnatmake: "variant.adb" compilation error

=8<=============================================================

procedure Variant is

   type Vehicle_Type is (CAR, LEGS, BIKE, PLANE);
   type Vehicle_Power is (MANUAL, AUTOMATIC);

   type Foo(Vehicle : Vehicle_Type; Power: Vehicle_Power) is
    record
        case Vehicle is
             when CAR  =>
            null;
             when LEGS =>
            null;
             when BIKE =>
            null;
             when PLANE =>
            null;
         when others =>
            null;
        end case;
    end record;

    Audi : Foo(Vehicle => MAN, Power => MANUAL);
begin
 null;
end Variant;

-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



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

end of thread, other threads:[~2004-02-17 15:43 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-14  0:00 Variant Records Shawn Barber
1999-09-14  0:00 ` Matthew Heaney
1999-09-14  0:00 ` Mark Lundquist
1999-09-14  0:00 ` David C. Hoos, Sr.
  -- strict thread matches above, loose matches on Subject: below --
2004-02-15 22:51 Variant records Ganesh Ramasivan
2004-02-16 10:57 ` Preben Randhol
2004-02-16 15:25   ` Ganesh Ramasivan
2004-02-16 15:46     ` Ludovic Brenta
2004-02-16 16:16     ` Preben Randhol
2004-02-17 14:37       ` Ganesh Ramasivan
2004-02-17 15:43         ` Preben Randhol
2004-02-16 16:59   ` Ganesh Ramasivan
2004-02-16 20:14     ` Simon Wright
2004-02-17 14:56       ` Ganesh Ramasivan
2004-02-17 15:31         ` Robert I. Eachus
2004-02-16 23:17 ` tmoran
1986-10-31  9:08 variant records Dean Luick
1986-11-03 14:17 ` Bev Sobelman
1986-11-05 16:35 ` stuart
1986-11-06 15:40 ` stt

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