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,a5d76152c5cb8790 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: class-wide objects Date: 1998/12/10 Message-ID: #1/1 X-Deja-AN: 420724619 Sender: matt@mheaney.ni.net References: <366B1190.C8142307@magic.fr> <366EE6BA.7D586BDD@magic.fr> NNTP-Posting-Date: Wed, 09 Dec 1998 23:21:20 PDT Newsgroups: comp.lang.ada Date: 1998-12-10T00:00:00+00:00 List-Id: Francois Godme writes: > Matthew Heaney writes: > > > Your type will probably have to have some sort of dispatching clone method: > > > > generic > > ... > > package Stacks is > > > > type Stack_Type is tagged limited private; > > > > type Stack_Access is access all Stack_Type'Class; > > > > function Copy (Stack : Stack_Type) return Stack_Access; > > ... > > end Stacks; > > > > which you'd use something like: > > > > procedure Op (Stack : in Stack_Type'Class) is > > > > Copy_Of_Stack : Stack_Access := Copy (Stack); > > > > begin > > The clone method is inventive but it is a very odd method in the sense > that when you inherit it, it is always wrong for you. So, you have to > redefine it. Then just make it non-primitive: generic ... package Stacks is type Stack_Type is ... ... package Constructors is function Copy (Stack : Stack_Type) return Stack_Access; end; ... end Stacks; Because Copy is non-primitive, it won't get inherited during derivation. > Usually, when you redefine a method, you call the inherited method on > the superclass part of you and finish the job yourself for the added > part. Here, the inherited method is not reusable because it clones > objects of your superclass not of yourself. You are the only one able > to clone yourself as your father knows nothing of you. In general, constructors shouldn't be primitive. > You are not forced to override this method by the language like the > language would do for an abstract method, so you can forget to do > it. You are on your own. Unless you make the operation non-primitive. > It's like a genetic disease that affects all generation. It can be > cured easily in one generation but the next generation will be > affected the same way. The disease cannot be stopped. It reappears > from generation to generation. Some generation are sane, some are > are sterile (they cannot reproduce themselves, they reproduce as their > parents.) There is no problem, if the operation isn't primitive. > The cure is always the same. It can be as simple as an Emacs Macro. > > function Copy (Stack : Stack_Type) return Stack_Access is > use type Ada.Tags.Tag; > begin > -- tries to be kind with sick children > if Stack_Type'Class (Stack)'Tag /= Stack_Type'Tag then > raise ...; -- Is that kindness ? I think it's better than return > null. > -- The family takes on responsabilities for its sick > -- children. > > else > return new Stack_Type; > end if; > end Copy; > > The attribute I was asking for does almost the same thing as the clone method > but without the associated problems. There are no problems with the approach I suggest above.