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.9 required=5.0 tests=BAYES_00,XPRIO autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5c573f4c7aebc763,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-02-07 14:02:23 PST Path: supernews.google.com!sn-xit-02!supernews.com!isdnet!wanadoo.fr!not-for-mail From: "Jean-Pierre Rosen" Newsgroups: comp.lang.ada Subject: Abstract null value Date: Wed, 7 Feb 2001 19:56:32 +0100 Organization: Wanadoo, l'internet avec France Telecom Message-ID: <95sgl4$3c8$2@wanadoo.fr> NNTP-Posting-Host: mix-tuileries-104-1-54.abo.wanadoo.fr X-Trace: wanadoo.fr 981583332 3464 193.250.183.54 (7 Feb 2001 22:02:12 GMT) X-Complaints-To: abuse@wanadoo.fr NNTP-Posting-Date: 7 Feb 2001 22:02:12 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Xref: supernews.google.com comp.lang.ada:4955 Date: 2001-02-07T22:02:12+00:00 List-Id: I recently came across this, so I wanted to share since it can be useful to others. FWIW... Imagine you have a tagged type Root_Type, from which you derive, etc., and have some constructor functions that return Root_Type'Class. Sometimes, the constructor is not able to perform the job, and you want to return some "null value" (I know, you can also raise an exception, but that's another story). How can you make an appropriate null value ? I realized that a differed constant could be of a class wide type.Therefore, the following is legal: package Abstract_Type is type Root_Type is abstract tagged null record; procedure Oper (X : Root_Type) is abstract; Null_Value : constant Root_Type'Class; private type Null_Type is new Root_Type with null record; procedure Oper (X : Null_Type); Null_Value : constant Root_Type'Class := Null_Type'(null record); end Abstract_Type; package body Abstract_Type is procedure Oper (X : Null_Type) is begin raise Constraint_Error; end Oper; end Abstract_Type; Now, the nice thing is that you get a public value whose (specific) type is not visible! Therefore, you are absolutely sure that outside the package, you cannot do anything with it, besides comparing for equality which is the intended purpose. If the root type has primitive operations (as above), you implement them as simply raising Constraint_Error, so that any dispatching on the Null_Value will raise Constraint_Error, which seems appropriate behavior. -- --------------------------------------------------------- J-P. Rosen (Rosen.Adalog@wanadoo.fr) Visit Adalog's web site at http://pro.wanadoo.fr/adalog