From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cfe54a7562aab2a1,start X-Google-Attributes: gid103376,public From: James_Rogers Subject: Factory Pattern in Ada Date: 1996/08/23 Message-ID: <321E17F6.167E@velveeta.apdev.cs.mci.com>#1/1 X-Deja-AN: 176040028 content-type: text/plain; charset=us-ascii organization: MCI Telecommunications Colorado Springs, CO mime-version: 1.0 newsgroups: comp.lang.ada x-mailer: Mozilla 2.0 (X11; I; AIX 2) Date: 1996-08-23T00:00:00+00:00 List-Id: 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