comp.lang.ada
 help / color / mirror / Atom feed
* variant record and pointer
@ 2003-05-02 17:25 Noivet
  2003-05-02 17:44 ` Robert A Duff
  2003-05-02 19:00 ` Frank J. Lhota
  0 siblings, 2 replies; 3+ messages in thread
From: Noivet @ 2003-05-02 17:25 UTC (permalink / raw)


Hello!

Maybe you can help me. I need a pointer with variant record. I have
something like this:
-------
type object is (circle, square, triangle);

type node;
type pointer is access node;
type pointer (index: object) is record
  ptr: pointer;
  special: unbounded_string;
  case index is
    when circle =>   diameter: natural;
    when square =>   side: natural;
    when triangle => edge_1, edge_2, edge_3: natural;  
  end case;
end record;
-------

now I can declare:
s, t: pointer;

and then use it:
s := new node(circle);
t := new node(square);
s.diameter := 5;
t.side := 3;
s.side := 10;   <------ and I wondered that the compiler compiled
successfully!
                        I expected a conflict, but there isn't.
                        is there something wrong with my declaration
of the pointer type? or can someone explain me that?


Thx!



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

* Re: variant record and pointer
  2003-05-02 17:25 variant record and pointer Noivet
@ 2003-05-02 17:44 ` Robert A Duff
  2003-05-02 19:00 ` Frank J. Lhota
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2003-05-02 17:44 UTC (permalink / raw)


Manu-CSS@gmx.de (Noivet) writes:

> s.side := 10;   <------ and I wondered that the compiler compiled
> successfully!
>                         I expected a conflict, but there isn't.
>                         is there something wrong with my declaration
> of the pointer type? or can someone explain me that?

There is a run-time check.  In general, the compiler can't know the
discriminant of s at compile time.

If you turn on warnings, a good compiler might warn, in some cases.
But in general, you have to run the program and trip over a
Constraint_Error.

- Bob



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

* Re: variant record and pointer
  2003-05-02 17:25 variant record and pointer Noivet
  2003-05-02 17:44 ` Robert A Duff
@ 2003-05-02 19:00 ` Frank J. Lhota
  1 sibling, 0 replies; 3+ messages in thread
From: Frank J. Lhota @ 2003-05-02 19:00 UTC (permalink / raw)


This is your question, but have you considered using tagged types instead of
variant records? You could declare your shape types as follows:

   type Shape;
   type Pointer is access all Shape'Class;

   type Shape is abstract tagged
      record
         Ptr     : Pointer;
         Special : Unbounded_String;
      end record;

   type Circle is new Shape with
      record
         Diameter : Natural;
      end record;

   type Square is new Shape with
      record
         Side : Natural;
      end record;

   type Triangle is new Shape with
      record
         Edge_1,
         Edge_2,
         Edge_3 : Natural;
      end record;

This would allow you to derive additional shapes (for example, Rectangle)
later, without impacting existing code. Moreover, you could write overloaded
subprograms that can dispatch at runtime, e.g.

    function Perimeter( X : in Shape ) return Float is abstract;

    function Perimeter( X : in Circle ) return Float;
    function Perimeter( X : in Square ) return Float;
    function Perimeter( X : in Triangle ) return Float;

    -- ...
    Pointer s;
    --

    Len := Perimeter( s.all );
    -- This calls the right Perimeter function,
    -- even if we cannot determine the type of s.all until runtime.





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

end of thread, other threads:[~2003-05-02 19:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-02 17:25 variant record and pointer Noivet
2003-05-02 17:44 ` Robert A Duff
2003-05-02 19:00 ` Frank J. Lhota

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