* Factory Pattern in Ada
@ 1996-08-23 0:00 James_Rogers
1996-08-25 0:00 ` Norman H. Cohen
0 siblings, 1 reply; 2+ messages in thread
From: James_Rogers @ 1996-08-23 0:00 UTC (permalink / 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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Factory Pattern in Ada
1996-08-23 0:00 Factory Pattern in Ada James_Rogers
@ 1996-08-25 0:00 ` Norman H. Cohen
0 siblings, 0 replies; 2+ messages in thread
From: Norman H. Cohen @ 1996-08-25 0:00 UTC (permalink / raw)
In article <321E17F6.167E@velveeta.apdev.cs.mci.com>, James_Rogers
<jrogers@velveeta.apdev.cs.mci.com> writes:
|> 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)?
Conversion from access-to-derived-type to access-to-parent-type is
allowed, but only if the access-to-parent type is a general
("access all") access type. Even then, the conversion must be written as
an explicit type conversion.
--
Norman H. Cohen ncohen@watson.ibm.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1996-08-25 0:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-23 0:00 Factory Pattern in Ada James_Rogers
1996-08-25 0:00 ` Norman H. Cohen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox