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,772ae8afc5db35f2 X-Google-Attributes: gid103376,public From: Samuel Mize Subject: Re: Can't export object of private type Date: 1999/02/25 Message-ID: <7b4517$2bbr@news3.newsguy.com>#1/1 X-Deja-AN: 448418602 References: <7b1k4h$13k6@news3.newsguy.com> Organization: ImagiNet Communications, Ltd. User-Agent: tin/pre-1.4-981002 ("Phobia") (UNIX) (AIX/3-2) Newsgroups: comp.lang.ada Date: 1999-02-25T00:00:00+00:00 List-Id: Don Harrison wrote: > Actually, I don't necessarily want to do that. What I'm really after > is polymorphic singletons - objects that > > 1) Have only one instance > 2) Are polymorphic > 3) Are visible to clients > 4) Can be called without a package prefix. > > I can acheive two or three, but not all four. :( I think I follow what you mean here. By "singleton" and (1) you mean that there can exist only one object of the type. But a visible type makes that impossible to ensure, so the approach you were taking is fatally flawed. Oh, the humanity. I would represent each singleton with a package, which gives 1 and 3, and use programming by extension to get points 2 and 4. Specifically, we start with: package Root_Singleton is type Flag is tagged null record; type Parameter_Type is (To_Be_Defined); procedure Action (Param: Flag; Other_Params_As_Needed: Parameter_Type); type Any_Singleton is access constant Flag'Class; Selected: aliased constant Flag := (null record); end Root_Singleton; Any_Singleton is a type for "flag" variables. It's access-to-constant so it can denote Selected'Access. In this package, Action does nothing. Perhaps you want it to raise an exception, since you should never call Action with Root_Singleton.Flag. Or, perhaps there is a meaningful "root" singleton for your hierarchy; I'm showing a general case. Anyway, next we can declare an arbitrary number of singletons: with Root_Singleton; package Venus is type Flag is new Root_Singleton.Flag with null record; procedure Action (Param: Flag; Other_Params_As_Needed: Root_Singleton.Parameter_Type); Selected: aliased constant Flag := (null record); end Venus; And so on for your other singletons. Now, the tagged types are NOT the singletons. Rather, items of the tagged types act as flags that let you specify the singleton to which you are referring. So, your user code can look like this: with Root_Singleton; use Root_Singleton; with Singleton_Picker; -- this function picks the right singleton for the current action, -- and returns Selected'Access from that singleton package procedure User is Selector: Any_Singleton; Params: Root_Singleton.Parameter_Type; begin loop Selector := Singleton_Picker; Action (Selector, Params); end loop; end User; Of course, if you don't need the return value elsewhere, you can eliminate "Selector" and just have loop Action (Singleton_Picker); end loop; Does that accomplish what you wanted? Let me guess. I bet "singleton" is a common object-oriented term, and it's usually considered to be a special case of a class. In Ada terms, it's just a package encapsulating some code and data. We can use Ada 95's programming-by-extension capability to execute the right procedure, based on a flag value. This isn't really an object-oriented concept at all, but many people think polymorphism is inherently tied up with object orientation. Did that commentary hit the mark, or did I mis-guess the background of the question? Best, Sam Mize -- Samuel Mize -- smize@imagin.net (home email) -- Team Ada Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam