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/24 Message-ID: <7b1k4h$13k6@news3.newsguy.com>#1/1 X-Deja-AN: 448022568 References: Organization: ImagiNet Communications, Ltd. User-Agent: tin/pre-1.4-981002 ("Phobia") (UNIX) (AIX/3-2) Newsgroups: comp.lang.ada Date: 1999-02-24T00:00:00+00:00 List-Id: Don Harrison wrote: > Looks like everyone's a bit coy on this question. :) Well, I didn't see any previous message, but maybe that's just me or my newsreader. You appear to want to export a variable of a private type, in the public part of the package that defines that type. The catch is that the full type definition must be available to the compiler (if not visible to the user) when a variable is declared, so it knows how much space to allocate. At the end of this message I show a workaround using a contained package. It ain't pretty, but it compiles. Note that the private part of the inner package is NOT visible to the rest of the outer package, so you should put any subprograms into the inner package. You can rename the elements of the inner package for the convenience of the end user. Of course, that user could also "use" the inner package. I didn't see anything important in the use of "tagged" for this question, so I left it out. Also, I wanted the package name to be meaningful. > The best workaround I've been able to come up with is to export an access > variable to the singleton. For example, Let me improve your access-based solution. Changes are in all-caps: > package Singleton is > > type Singleton_Type is tagged private; > type Access_Singleton_Type is access all Singleton_Type'Class; > > procedure Do_Something (S: in Singleton_Type); > > S_Ptr: CONSTANT Access_Singleton_Type; > > private > type Singleton_Type is tagged null record; > S: aliased Singleton_Type; S_PTR: CONSTANT ACCESS_SINGLETON_TYPE := S'ACCESS; > end; This way, users of the package don't have to wait for the elaboration of Singleton's BODY for S_Ptr to denote S -- it does so once the SPEC has been elaborated. (E.g, a package spec that "withs" Singleton could do something meaningful with it.) (It would be smart to initialize S.) Also, malicious or careless users can't assign a new value to S_Ptr, for instance one denoting a procedure's local variable: with Singleton; procedure Mess_Everybody_Up is S: Singleton_Type; begin S_Ptr := S'Unchecked_Access; -- can't do it with S_Ptr constant! end Mess_Everybody_Up; Best, Sam Mize - - - - - - package Meaningful is package Inner is type The_Type is private; procedure The_Procedure (Param: The_Type); An_Instance_Initial_Value: constant The_Type; private type The_Type is new integer; An_Instance_Initial_Value: constant The_Type := 1; end Inner; -------------- -- RENAMING DECLARATIONS FOR END-USER VISIBILITY -------------- subtype The_Type is Inner.The_Type; procedure The_Procedure (Param: The_Type) renames Inner.The_Procedure; -------------- -- HERE FINALLY IS YOUR EXPORTED PRIVATE VARIABLE OBJECT -------------- An_Instance: The_Type := Inner.An_Instance_Initial_Value; end Meaningful; -- Samuel Mize -- smize@imagin.net (home email) -- Team Ada Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam