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,e5eb8ca5dcea2827 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Ada OO Mechanism Date: 1999/05/28 Message-ID: <7imjg9$83r@sjx-ixn6.ix.netcom.com>#1/1 X-Deja-AN: 483181794 References: <7i05aq$rgl$1@news.orbitworld.net> <7i17gj$1u1k@news2.newsguy.com> <7icgkg$k4q$1@nnrp1.deja.com> <3749E9EC.2842436A@aasaa.ofe.org> <7id2eo$fag@drn.newsguy.com> <3749FF7D.F17CE16A@aasaa.ofe.org> <374AC676.F7AE0772@lmco.com> <7ieuja$5v9@news1.newsguy.com> <7ik1e5$8bc@news1.newsguy.com> <86lne9qh28.fsf@ppp-159-70.villette.club-internet.fr> Organization: Netcom X-NETCOM-Date: Fri May 28 10:24:25 AM PDT 1999 Newsgroups: comp.lang.ada Date: 1999-05-28T10:24:25-07:00 List-Id: In article <86lne9qh28.fsf@ppp-159-70.villette.club-internet.fr>, Laurent Guerby wrote: >Hyman Rosen writes: >> [...] >> From reading the Rationale, I thought that the assignment to z would >> result in a dispatching call to Get, using the tag of z that was set >> at its initialization. What am I doing wrong here? How do controlling >> return types work? > >A variable, once given an initial value, never change its type in >Ada. So the assignment to a class wide variable can only be done in a >declarative part. > >--LG Declarative parts can appear in subprograms or declare blocks. Therefore, one can take advantage of the ability to declare classwide variables in some interesting ways. Given a tagged type, type Machine is tagged ... with derviations, type Big_Machine is new Machine with ... type Solenoid is new Machine with ... type Tractor is new Big_Machine with ... one could dynamically dispatch to some operation overridden for each kind of machine such as, procedure Turn_On(M : in out Machine); If we also provide a classwide Get operation that retrieves objects of type Machine'Class from some kind of container, function Get (C : in Container) return Machine'Class; then we can have a loop that looks like this, loop declare M : Machine'Class := Get(container-name); -- constrains M with classwide Get function begin Turn_On(M); end; end loop; where the classwide variable will be constrained by the Get operation each time through the loop. This technique can be used in subprograms, tasks, or declare blocks. I have an example I use in some of my classes that demonstrates several variations on this for Stream_IO files. For Stream_IO you would want to use Machine'Class'Input(filename). It works quite nicely and is simple to implement and understand. Richard Riehle richard@adaworks.com http://www.adaworks.com