comp.lang.ada
 help / color / mirror / Atom feed
* Abstract null value
@ 2001-02-07 18:56 Jean-Pierre Rosen
  2001-02-08 15:17 ` Ted Dennison
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-Pierre Rosen @ 2001-02-07 18:56 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-02-08 18:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-07 18:56 Abstract null value Jean-Pierre Rosen
2001-02-08 15:17 ` Ted Dennison
2001-02-08 18:24   ` Jean-Pierre Rosen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox