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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,62a0e3de4e2ff2c7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-03-21 12:15:29 PST Newsgroups: comp.lang.ada Path: nntp.gmd.de!dearn!blekul11!ccsdec1.ufsia.ac.be!reks.uia.ac.be!idefix.CS.kuleuven.ac.be! Belgium.EU.net!EU.net!howland.reston.ans.net!gatech!purdue!news.bu.edu!inmet!henning!stt From: stt@henning.camb.inmet.com (Tucker Taft) Subject: Re: Tagged type: how to know which specific type Message-ID: Sender: news@inmet.camb.inmet.com Organization: Intermetrics, Inc. X-Newsreader: TIN [version 1.1 PL8] References: Date: Tue, 21 Mar 1995 14:38:28 GMT Date: 1995-03-21T14:38:28+00:00 List-Id: 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 : Type Diesel is new fuel with : 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.