comp.lang.ada
 help / color / mirror / Atom feed
* (elementary question) Test on type ?
@ 2001-09-04  8:41 Reinert Korsnes
  2001-09-04  9:18 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 20+ messages in thread
From: Reinert Korsnes @ 2001-09-04  8:41 UTC (permalink / raw)


Hi,
 
is it possible in Ada to test on type ? (did not manage to find out)
 
 
Assume:
 
 
-- specification :
generic
  type T is (<>);
function F(X : T) return Float;
 
--body :
 
function F(X : T) return Float is
begin
 
if Type(X) = Integer then  ??????
   something...
 
I := integer(X) ????
 
 
end;
 
--application prog:
 
function F1 is new F(T => Integer);


reinert

-- 
http://home.chello.no/~rkorsnes



^ permalink raw reply	[flat|nested] 20+ messages in thread
* RE: (elementary question) Test on type ?
@ 2001-09-04 17:55 Beard, Frank
  2001-09-05  9:16 ` John McCabe
  0 siblings, 1 reply; 20+ messages in thread
From: Beard, Frank @ 2001-09-04 17:55 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

I agree with what John's saying about "Generics are the place to do
identical stuff ...", but I don't think you need to pass overloaded
subprograms and some enumeration types to do specifics.  The generic
formal subprograms are for the specific stuff.  So, I think all you
would need is the following (from his original post):

-- specification :
generic
  type T is (<>);
  with procedure Do_Something (Item : in out T);
  with function Specific_Stuff (Item : T) return Float;
function F(X : T) return Float;
 
--body :
 
function F(X : T) return Float is
begin
 
--<Replace this>
--if Type(X) = Integer then  ??????
--   something...
-- 
--I := integer(X) ????

--<with this>
  Do_Something(X);

  -- if you really need to convert to integer here.
  I := integer(Specific_Stuff(X));

  return Specific_Stuff(X);
 
 
end;
 
--application prog:

procedure Do_Something (to_The_Integer : in out integer);

function To_Float(from_Integer : integer) return Float;

function F1 is new F(T              => Integer,
                     Do_Something   => Do_Something,
                     Specific_Stuff => To_Float);

The decisions are already made in the subprograms being used
to instantiate the generic, so it is unnecessary to do it again
internally in the generic.  Whatever specific things you want to
do based on the types should be abstracted out to the actual
subprograms that are supplied for the generic formal parameters.

Frank

-----Original Message-----
From: john.mccabe@emrad.com.nospam [mailto:john.mccabe@emrad.com.nospam]

Generics are the place to do identical stuff independent of the types.
If you want to do different stuff depending on the type, then you
should use overloaded function. I'm not sure whether this is possible
(and I can't check at the moment as I don't use Ada any more), but you
may be able to pass overloaded functions as generic actual parameters
and then just make a call where the compiler will handle the types.

e.g.

generic
   type MyType is (<>);
   with procedure Test (InParm : in Integer);
   with procedure Test (InParm : in SomeOtherDiscreteType);
package X
   :

Then call Test when you want. I guess what you could do was define a
type somewhere e.g: type PossibleTypes is (Int, Flt, Enum); then use
declare the Test operations as functions that returned that type.

Hope this helps.



Best Regards
John McCabe
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada



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

end of thread, other threads:[~2001-09-06 17:10 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-04  8:41 (elementary question) Test on type ? Reinert Korsnes
2001-09-04  9:18 ` David C. Hoos, Sr.
2001-09-04  9:29   ` Reinert Korsnes
2001-09-04 11:02     ` Jacob Sparre Andersen
2001-09-04 11:05       ` Reinert Korsnes
2001-09-04 11:39         ` John McCabe
2001-09-04 13:30         ` Marin David Condic
2001-09-04 14:07           ` Ted Dennison
2001-09-04 14:48             ` Marin David Condic
2001-09-04 18:35               ` Mark Biggar
2001-09-04 19:33                 ` Marin David Condic
2001-09-04 14:15         ` Ted Dennison
2001-09-05  9:14           ` John McCabe
2001-09-05 14:19             ` Ted Dennison
2001-09-05 16:24               ` John McCabe
2001-09-05 18:33               ` Ehud Lamm
2001-09-06  9:36           ` Reinert Korsnes
2001-09-06 17:10             ` (elementary question) Test on type ? Pragma inline(granularity)? Warren W. Gay VE3WWG
  -- strict thread matches above, loose matches on Subject: below --
2001-09-04 17:55 (elementary question) Test on type ? Beard, Frank
2001-09-05  9:16 ` John McCabe

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