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

* Re: Abstract null value
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Ted Dennison @ 2001-02-08 15:17 UTC (permalink / raw)


In article <95sgl4$3c8$2@wanadoo.fr>,
  "Jean-Pierre Rosen" <rosen.adalog@wanadoo.fr> wrote:

> 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
...
> 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;

For this system to work, all your "constructors" would have to be
functions returning something like Root_Type'Class, and their targets
would all have to be class-wide initializations or allocators, right? Or
is that some kind of unwritten standard of which I'm unaware? A slim
majority of the time when I'm "constructing" a tagged type object
there's a specific type the expression is expecting. If I were to do
that with the above system, then I'd just get Constraint_Error.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Abstract null value
  2001-02-08 15:17 ` Ted Dennison
@ 2001-02-08 18:24   ` Jean-Pierre Rosen
  0 siblings, 0 replies; 3+ messages in thread
From: Jean-Pierre Rosen @ 2001-02-08 18:24 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1493 bytes --]

"Ted Dennison" <dennison@telepath.com> a �crit dans le message news: 95uda1$pq6$1@nnrp1.deja.com...
> In article <95sgl4$3c8$2@wanadoo.fr>,
>   "Jean-Pierre Rosen" <rosen.adalog@wanadoo.fr> wrote:
>
> > 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
> ...
> > 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;
>
> For this system to work, all your "constructors" would have to be
> functions returning something like Root_Type'Class, and their targets
> would all have to be class-wide initializations or allocators, right? Or
> is that some kind of unwritten standard of which I'm unaware? A slim
> majority of the time when I'm "constructing" a tagged type object
> there's a specific type the expression is expecting. If I were to do
> that with the above system, then I'd just get Constraint_Error.

The kind of constructor I was thinking about is one that, for example, reads a file and returns various objects (widgets...). If you
have a syntax error, or the end of file, you may want to return a null value. I'm not saying it's applicable to everything, just
that it can be useful in some cases.

--
---------------------------------------------------------
           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