comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Re: Tagged type: how to know which specific type
Date: Tue, 21 Mar 1995 14:38:28 GMT
Date: 1995-03-21T14:38:28+00:00	[thread overview]
Message-ID: <D5soo5.FLG@inmet.camb.inmet.com> (raw)
In-Reply-To: WOODRUFF.95Mar18114334@tanana.llnl.gov

woodruff@tanana.llnl.gov wrote:
: I've been puzzling for some time about the proper use of tagged types when
: I wish to have information about what type of object I'm modeling, without
: having the object itself.

: In the olden days using a discriminated record type I would have an easy
: solution because the discriminant itself was a visible type, and I could
: use a value of that type to give information about a record instance.

: Let me pose a problem that I think illustrates my puzzle; I've had
: excellent advice from my c.l.a friends in the past and I hope that will
: happen again!

: Suppose my discourse is about vehicles and their fuels; I'll just talk
: about the fuel here:

: Type fuel is tagged <whatever>
:   Type Diesel is new fuel with <whatever>
:   Type Gasoline is new fuel .....

: So if I want to put fuel in my car, how can I be sure I get the right
: stuff?  Naively I imagine going to the pump, reading the label on the
: pump, and dispensing fuel iff the pump has gasoline.  How do I ask the
: pump what kind it has without dispensing (more formally, what kind of
: answer will I get if I ask that question of the pump)?  I don't want to
: take some fuel if it's the wrong kind, and I sure don't want to pour the
: generic stuff into my car, and depend on the car to raise an exception
: if it's not gasoline.

You could have a function that returned a Tag (of type Ada.Tags.Tag),
and then compare it with the 'Tag attribute of some specific type.
E.g.:
    function Fuel_Tag(Pump : Pump_Type) return Ada.Tags.Tag;
    if Fuel_Tag(Some_Pump) /= Gasoline'Tag then
        Put_Line("No thank you.");
    else
        Put_Line("Fill 'er up!");
        Fill(My_Car, From => Some_Pump);
    end if;

: To go even further, suppose I'm in the filling station business.  The
: OSHA has just made a new regulation: for every chemical I have in
: inventory, I'm required to have a document - the Material Safety Data
: Sheet - that carries emergency information about toxicity etc.

: So now I call the official printer and ask for copies of the MSDS for
: gasoline.  How do I say which MSDS I want?  It seems if I hope to use
: the language to assure that my model is correct, that I must send some
: gasoline to the printing plant(!)

You can get a printable name for a tag using either Ada.Tags.External_Tag
or Ada.Tags.Expanded_Name.  An external tag (of type String) can be
converted back to an internal tag (of type Ada.Tags.Tag) using Internal_Tag.

See RM95 3.9(5-18) for more details on the above.

Note also that a membership test can be used to see if a particular
object belongs to a specific type or to a class of types, e.g.:

    if Some_Fuel in Gasoline'Class then ...

: John Woodruff
: Lawrence Livermore National Lab
: 510 422 4661

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



      parent reply	other threads:[~1995-03-21 14:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-18 19:43 Tagged type: how to know which specific type woodruff
1995-03-19 22:27 ` Robert Dewar
1995-03-21 14:38 ` Tucker Taft [this message]
replies disabled

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