comp.lang.ada
 help / color / mirror / Atom feed
From: James_Rogers <jrogers@velveeta.apdev.cs.mci.com>
Subject: Factory Pattern in Ada
Date: 1996/08/23
Date: 1996-08-23T00:00:00+00:00	[thread overview]
Message-ID: <321E17F6.167E@velveeta.apdev.cs.mci.com> (raw)


In studying Object Oriented design patterns I have come across
a problem which I hope a more experienced Ada designer can help
me around.

When trying to implement a Factory pattern I am stuck with the
need to employ extension aggregates.  Example

with Ada.Finalization;

Package node is

   type node is abstract tagged private;
   type node_Access is access all node'class;

   type tree is new Ada.Finalization.Controlled with record
      Element : Node_Access;
   end record;

   type Tree_Access is access all Tree;

private

   type node is abstract tagged record
      Ref_Counter : Natural := 1;
   end record;
end node;

------------

with node; use node;

package real_nodes is

   type int_node is new node with record
      Num : Integer;
   end record;

   type int_node_access is access all int_node;

   type unary_node is new node with record
      Operation : Character;
      Operand   : Tree_Access;
   end record;

   type binary_node is new node with record
      Operation : Character;
      Left      : Tree_Access;
      Right     : Tree_Access;
   end record;

   ......
end real_nodes;

When I try to construct a tree the GNAT compiler tells me that I
need to use extension aggregates as in the following:

t1 : Tree_Access := new Tree'(Ada.Finalization.Controlled with
                    Node => new Binary_Node'( Node.Node with
                    Operation => '+',
                    Left => new Tree'(Ada.Finalization.Controlled with
                    Node => new Int_Node'(Node.Node with
                    Num => 5)),
                    Right => new Tree'(Ada.Finalization.Controlled with
                    Node => new Int_Node'(Node.Node with
                    Num => 4))));

I would like to create functions along the lines of:

  function new_node (Num : in Integer) return Int_Node_Access;
  function new_node (Operation : in Character;
                     Operand   : in Tree_Access) 
                                     return Unary_Node_Access;
  function new_node (Operation : in Character;
                     Left      : in Tree_Access;
                     Right     : in Tree_Access) 
                                    return Binary_Node_Access;

When I do this, the compiler complains that it found type 
Binary_Node_Access when it expected type Node_Access.

Since Binary_Node is a descendant of Node, why does the compiler
complain about the assignment of a value of Binar_Node_Access to
a type of Node_Access (as defined above)?

-- 
Jim Rogers
*************************************************************
Celebrate Diplomacy




             reply	other threads:[~1996-08-23  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-08-23  0:00 James_Rogers [this message]
1996-08-25  0:00 ` Factory Pattern in Ada Norman H. Cohen
replies disabled

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