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  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
  0 siblings, 1 reply; 20+ messages in thread
From: David C. Hoos, Sr. @ 2001-09-04  9:18 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Reinert.Korsnes

You would say instead:

 if X in Integer then

See section 4.5.2 of the Ada Language Reference Manual.

----- Original Message ----- 
From: "Reinert Korsnes" <Reinert.Korsnes@ffi.no>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: September 04, 2001 3:41 AM
Subject: (elementary question) Test on type ?


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

* Re: (elementary question) Test on type ?
  2001-09-04  9:18 ` David C. Hoos, Sr.
@ 2001-09-04  9:29   ` Reinert Korsnes
  2001-09-04 11:02     ` Jacob Sparre Andersen
  0 siblings, 1 reply; 20+ messages in thread
From: Reinert Korsnes @ 2001-09-04  9:29 UTC (permalink / raw)


David C. Hoos, Sr. wrote:

> You would say instead:
> 
>  if X in Integer then

I get error message "incompatible types".....

reinert

> 
> See section 4.5.2 of the Ada Language Reference Manual.
> 
> ----- Original Message -----
> From: "Reinert Korsnes" <Reinert.Korsnes@ffi.no>
> Newsgroups: comp.lang.ada
> To: <comp.lang.ada@ada.eu.org>
> Sent: September 04, 2001 3:41 AM
> Subject: (elementary question) Test on type ?
> 
> 
> 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  9:29   ` Reinert Korsnes
@ 2001-09-04 11:02     ` Jacob Sparre Andersen
  2001-09-04 11:05       ` Reinert Korsnes
  0 siblings, 1 reply; 20+ messages in thread
From: Jacob Sparre Andersen @ 2001-09-04 11:02 UTC (permalink / raw)


Reinert:

> >  if X in Integer then
> 
> I get error message "incompatible types".....

Then X is apparently not of type Integer. ;-)

Seriously, what are you trying to do?

Jacob
-- 
"Human beings just can't not communicate."



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

* Re: (elementary question) Test on type ?
  2001-09-04 11:02     ` Jacob Sparre Andersen
@ 2001-09-04 11:05       ` Reinert Korsnes
  2001-09-04 11:39         ` John McCabe
                           ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Reinert Korsnes @ 2001-09-04 11:05 UTC (permalink / raw)


Jacob Sparre Andersen wrote:

> Reinert:
> 
>> >  if X in Integer then
>> 
>> I get error message "incompatible types".....
> 
> Then X is apparently not of type Integer. ;-)
> 
> Seriously, what are you trying to do?

Just do a bit different things in the function depending on the type
of X.   Say, X is Float of Interger....

Maybe some dirty programming, but anyway.

reinert

> 
> Jacob

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



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

* Re: (elementary question) Test on type ?
  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:15         ` Ted Dennison
  2 siblings, 0 replies; 20+ messages in thread
From: John McCabe @ 2001-09-04 11:39 UTC (permalink / raw)


On Tue, 4 Sep 2001 13:05:21 +0200, Reinert Korsnes
<Reinert.Korsnes@ffi.no> wrote:

>> Seriously, what are you trying to do?
>
>Just do a bit different things in the function depending on the type
>of X.   Say, X is Float of Interger....

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



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

* Re: (elementary question) Test on type ?
  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:15         ` Ted Dennison
  2 siblings, 1 reply; 20+ messages in thread
From: Marin David Condic @ 2001-09-04 13:30 UTC (permalink / raw)


What you are trying to do is basically impossible in Ada. You can't have a
generic parameter that is, say, "private" and then try to test to see if the
actual is of type Integer or Float. There are a couple of reasons for this:

One is that there are an infinite number of possible Integer types. You can
declare your own integer types all day long, so how can you build into the
code some sort of test to decide if the type is "Integer" and have that mean
anything? Either it has to test for the *specific* type "Standard.Integer"
in which case it misses all the other infinite number of possible integer
types you can create, or it has to look for all things that exhibit the
characteristics of integers. If the latter is the case, then you're down to:

type X is range <> ;

That way you know it can only be filled by an actual that satisfies the
characteristics of integer types.

But you want to also supply floating point types? Well, then you can't
assume the characteristics of both integer types *and* floating point types
because the sets of characteristics are different in many areas. (There is
no Integer'Machine_Mantissa, for example) You have to go to the next level
up and abstract away everything but the things that are common to both
integer and floating point types. That would be a "private" formal type. You
can't assume math ops or attributes because they are different between
floating and discrete types - so you can only assume what is common to both.

I think this illustrates a minor "hole" in the Ada language design. It would
be nice to have a generic parameter that was somewhere between "private" and
integer, decimal, float, etc. It would be nice to say: "type X is scalar ;"
or similar and know that you can presume "+", "-", and other common math
operations, but not necessarily those that make no sense for all
mathematical types. Just a thought...

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Reinert Korsnes" <Reinert.Korsnes@ffi.no> wrote in message
news:9n2ctk$36v$1@snipp.uninett.no...
> Jacob Sparre Andersen wrote:
>
> > Reinert:
> >
> >> >  if X in Integer then
> >>
> >> I get error message "incompatible types".....
> >
> > Then X is apparently not of type Integer. ;-)
> >
> > Seriously, what are you trying to do?
>
> Just do a bit different things in the function depending on the type
> of X.   Say, X is Float of Interger....
>
> Maybe some dirty programming, but anyway.
>
> reinert
>
> >
> > Jacob
>
> --
> http://home.chello.no/~rkorsnes





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

* Re: (elementary question) Test on type ?
  2001-09-04 13:30         ` Marin David Condic
@ 2001-09-04 14:07           ` Ted Dennison
  2001-09-04 14:48             ` Marin David Condic
  0 siblings, 1 reply; 20+ messages in thread
From: Ted Dennison @ 2001-09-04 14:07 UTC (permalink / raw)


In article <9n2l16$bqv$1@nh.pace.co.uk>, Marin David Condic says...
>
>What you are trying to do is basically impossible in Ada. You can't have a
>generic parameter that is, say, "private" and then try to test to see if the
>actual is of type Integer or Float. There are a couple of reasons for this:

Actually it is doable, if the type in question is a tagged type. In that case
you can easily do something like:

if Object_A in Parent_Type_X'class then ....

The reason this won't work for Integer is that Integer isn't tagged. Thus:

1) You can't use 'Class on it (or a subtype of it).
2) You can't pass it into a routine that can also take other types for the same
parameter.

I suppose you would be able to do that with Integer if Ada had a type system
where every type was derived from some common root type. I think there are some
languages out there that work that way. Ada just isn't one of them.


---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: (elementary question) Test on type ?
  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:15         ` Ted Dennison
  2001-09-05  9:14           ` John McCabe
  2001-09-06  9:36           ` Reinert Korsnes
  2 siblings, 2 replies; 20+ messages in thread
From: Ted Dennison @ 2001-09-04 14:15 UTC (permalink / raw)


In article <9n2ctk$36v$1@snipp.uninett.no>, Reinert Korsnes says...
>
>Jacob Sparre Andersen wrote:
>> Seriously, what are you trying to do?
>
>Just do a bit different things in the function depending on the type
>of X.   Say, X is Float of Interger....

But you can't specify such a parameter, unless you are working with generics (is
that it?).

Again, what exactly are you trying to do? Or, is this entirely theorietical?

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: (elementary question) Test on type ?
  2001-09-04 14:07           ` Ted Dennison
@ 2001-09-04 14:48             ` Marin David Condic
  2001-09-04 18:35               ` Mark Biggar
  0 siblings, 1 reply; 20+ messages in thread
From: Marin David Condic @ 2001-09-04 14:48 UTC (permalink / raw)


O.K. O.K. O.K. - You got me there. But that's sort of a deeper technical
area than what I think the original question was aimed at. (Sounded more
like a newbie with a more basic question than might be answered by tagged
records)

I'd agree that if Ada were more of a "purely OO language" it probably would
have started deriving all of its types from some base type & generic
parameters might have had more adaptable characteristics. Its been
frustrating for me to deal with certain math-oriented packages where I've
essentially had to create more-or-less identical code for integer types,
fixed types, floating types and decimal types. Lots of math can be done
sticking only to the operations common to all of the above and if they were
all derived from some class called "Scalar" (or had a suitable generic
formal) you could get there a lot easier.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Ted Dennison" <dennison@telepath.com> wrote in message
news:le5l7.3173$4z.4905@www.newsranger.com...
>
> Actually it is doable, if the type in question is a tagged type. In that
case
> you can easily do something like:
>
> if Object_A in Parent_Type_X'class then ....
>
> The reason this won't work for Integer is that Integer isn't tagged. Thus:
>
> 1) You can't use 'Class on it (or a subtype of it).
> 2) You can't pass it into a routine that can also take other types for the
same
> parameter.
>
> I suppose you would be able to do that with Integer if Ada had a type
system
> where every type was derived from some common root type. I think there are
some
> languages out there that work that way. Ada just isn't one of them.
>






^ 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

* Re: (elementary question) Test on type ?
  2001-09-04 14:48             ` Marin David Condic
@ 2001-09-04 18:35               ` Mark Biggar
  2001-09-04 19:33                 ` Marin David Condic
  0 siblings, 1 reply; 20+ messages in thread
From: Mark Biggar @ 2001-09-04 18:35 UTC (permalink / raw)


Marin David Condic wrote:
> 
> O.K. O.K. O.K. - You got me there. But that's sort of a deeper technical
> area than what I think the original question was aimed at. (Sounded more
> like a newbie with a more basic question than might be answered by tagged
> records)
> 
> I'd agree that if Ada were more of a "purely OO language" it probably would
> have started deriving all of its types from some base type & generic
> parameters might have had more adaptable characteristics. Its been
> frustrating for me to deal with certain math-oriented packages where I've
> essentially had to create more-or-less identical code for integer types,
> fixed types, floating types and decimal types. Lots of math can be done
> sticking only to the operations common to all of the above and if they were
> all derived from some class called "Scalar" (or had a suitable generic
> formal) you could get there a lot easier.
> 
> MDC
> --
> Marin David Condic
> Senior Software Engineer
> Pace Micro Technology Americas    www.pacemicro.com
> Enabling the digital revolution
> e-Mail:    marin.condic@pacemicro.com
> Web:      http://www.mcondic.com/
> 
> "Ted Dennison" <dennison@telepath.com> wrote in message
> news:le5l7.3173$4z.4905@www.newsranger.com...
> >
> > Actually it is doable, if the type in question is a tagged type. In that
> case
> > you can easily do something like:
> >
> > if Object_A in Parent_Type_X'class then ....
> >
> > The reason this won't work for Integer is that Integer isn't tagged. Thus:
> >
> > 1) You can't use 'Class on it (or a subtype of it).
> > 2) You can't pass it into a routine that can also take other types for the
> same
> > parameter.
> >
> > I suppose you would be able to do that with Integer if Ada had a type
> system
> > where every type was derived from some common root type. I think there are
> some
> > languages out there that work that way. Ada just isn't one of them.
> >

You can do almost what you want using a generic package using a private
generic type and explicitly defining the operators you want to use.
For example if your package only needs "+", "*", "-", "=", "<".

generic
	type Scalar is private;
	with function "+"(Left, Right: Scalar) return Scalar is <>;
	with function "="(Left, Right: Scalar) return Boolean is <>;
	with function "-" ... ;
	with function "<" ... ;
package Special_Math is
	...
end Special_Math;

package Special_Int is new Special_Math(Scalar => Integer);
package Special_Float is new Special_Math(Scalar => Float);

--
Mark Biggar
mark.a.biggar@home.com



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

* Re: (elementary question) Test on type ?
  2001-09-04 18:35               ` Mark Biggar
@ 2001-09-04 19:33                 ` Marin David Condic
  0 siblings, 0 replies; 20+ messages in thread
From: Marin David Condic @ 2001-09-04 19:33 UTC (permalink / raw)


You can get there via a variety of ways. One of my favorites is to declare
an abstract tagged private type that has "+", "-", etc. as abstract
functions. You can then use a generic formal that is of the 'Class of that
tagged type. A user then derives from the abstract class and supplies the
operations. That saves you from making a long generic formal list at the
expense of making a long abstract function list. Don't know that there is a
huge advantage to either one.

But none of the possible solutions is as "clean" as simply having a "type X
is scalar ;" generic formal parameter. (or some similar mechanism.) If Ada
is smart enough to detect the operations for the actual parameter anyway,
then maybe I don't have to do the work. That's always A Good Thing.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Mark Biggar" <mark.a.biggar@home.com> wrote in message
news:3B951F33.927CA445@home.com...
>
> You can do almost what you want using a generic package using a private
> generic type and explicitly defining the operators you want to use.
> For example if your package only needs "+", "*", "-", "=", "<".
>
> generic
> type Scalar is private;
> with function "+"(Left, Right: Scalar) return Scalar is <>;
> with function "="(Left, Right: Scalar) return Boolean is <>;
> with function "-" ... ;
> with function "<" ... ;
> package Special_Math is
> ...
> end Special_Math;
>
> package Special_Int is new Special_Math(Scalar => Integer);
> package Special_Float is new Special_Math(Scalar => Float);
>






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

* Re: (elementary question) Test on type ?
  2001-09-04 14:15         ` Ted Dennison
@ 2001-09-05  9:14           ` John McCabe
  2001-09-05 14:19             ` Ted Dennison
  2001-09-06  9:36           ` Reinert Korsnes
  1 sibling, 1 reply; 20+ messages in thread
From: John McCabe @ 2001-09-05  9:14 UTC (permalink / raw)


On Tue, 04 Sep 2001 14:15:19 GMT, Ted Dennison<dennison@telepath.com>
wrote:

>In article <9n2ctk$36v$1@snipp.uninett.no>, Reinert Korsnes says...
>>
>>Jacob Sparre Andersen wrote:
>>> Seriously, what are you trying to do?
>>
>>Just do a bit different things in the function depending on the type
>>of X.   Say, X is Float of Interger....
>
>But you can't specify such a parameter, unless you are working with generics (is
>that it?).

I don't know whether you missed the original message, but it
contained:

> Assume:
>  
>  
> -- specification :
> generic
>   type T is (<>);
> function F(X : T) return Float;
>  

so it appears the intention was related to the use of generics.



Best Regards
John McCabe



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

* Re: (elementary question) Test on type ?
  2001-09-04 17:55 (elementary question) Test on type ? Beard, Frank
@ 2001-09-05  9:16 ` John McCabe
  0 siblings, 0 replies; 20+ messages in thread
From: John McCabe @ 2001-09-05  9:16 UTC (permalink / raw)


Frank

I believe you are correct here, that makes sense. As I mentioned, I'm
nor using Ada any more so what I was saying couldn't be relied on (but
hopefully was just a pointer in the right direction :-)

On Tue, 4 Sep 2001 13:55:27 -0400, "Beard, Frank" 
<beardf@spawar.navy.mil> wrote:

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


Best Regards
John McCabe



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

* Re: (elementary question) Test on type ?
  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
  0 siblings, 2 replies; 20+ messages in thread
From: Ted Dennison @ 2001-09-05 14:19 UTC (permalink / raw)


In article <3b95eca7.5408987@news.demon.co.uk>, John McCabe says...
>
>I don't know whether you missed the original message, but it
>contained:
>
>> Assume:
>>  
>>  
>> -- specification :
>> generic
>>   type T is (<>);
>> function F(X : T) return Float;

Ahh, you're right; I did miss that.


This issue does actually come once in a while too, when you've got code that
needs to be mostly the same, except for one little bit. I think you pretty much
have to hack it. Approaches I have seen:

Make one generic for each type.
Variant 1: Copy and pase the code (maintainence problems).
Variant 2: Use a common subprogram for the common code (cool, but only works if
you don't need to use the generic part in the common code).

Make one generic, hack the difference.
Hack 1: Make the user supply a boolean designating which code path to take.
(flag coupling - fairly bad)
Hack 2: Make the user supply the non-common code themselves as a procedure
parameter. (works well if the non-common stuff can be put together, and it
doesn't rely on package-internal stuff, and you can trust the clients to get
this code right. Not so good if its stuff the clients shouldn't have to worry
about doing themselves.


Hack 1 is really just a special case of Hack 2, where the routine being supplied
is an "Is_Integer" routine, simplified to a boolean. Somewhere in between these
two extremes you may find a good spot.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: (elementary question) Test on type ?
  2001-09-05 14:19             ` Ted Dennison
@ 2001-09-05 16:24               ` John McCabe
  2001-09-05 18:33               ` Ehud Lamm
  1 sibling, 0 replies; 20+ messages in thread
From: John McCabe @ 2001-09-05 16:24 UTC (permalink / raw)


On Wed, 05 Sep 2001 14:19:55 GMT, Ted Dennison<dennison@telepath.com>
wrote:

>Variant 1: Copy and pase the code (maintainence problems).

Mmm  :-) Would that be "Copy and paste".



Best Regards
John McCabe



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

* Re: (elementary question) Test on type ?
  2001-09-05 14:19             ` Ted Dennison
  2001-09-05 16:24               ` John McCabe
@ 2001-09-05 18:33               ` Ehud Lamm
  1 sibling, 0 replies; 20+ messages in thread
From: Ehud Lamm @ 2001-09-05 18:33 UTC (permalink / raw)


Ted Dennison <dennison@telepath.com> wrote in message
news:fwql7.4754$4z.14897@www.newsranger.com...
> Hack 2: Make the user supply the non-common code themselves as a procedure
> parameter. (works well if the non-common stuff can be put together, and it
> doesn't rely on package-internal stuff, and you can trust the clients to
get
> this code right. Not so good if its stuff the clients shouldn't have to
worry
> about doing themselves.
>

You can refine this using a layerd design, so that the client is shielded
from the internal details, by adding a top layer that uses the generic, and
defines the required subprogram parameters . In some cases child units may
come in handy (if you must rely on internal deails).

Ehud Lamm





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

* Re: (elementary question) Test on type ?
  2001-09-04 14:15         ` Ted Dennison
  2001-09-05  9:14           ` John McCabe
@ 2001-09-06  9:36           ` Reinert Korsnes
  2001-09-06 17:10             ` (elementary question) Test on type ? Pragma inline(granularity)? Warren W. Gay VE3WWG
  1 sibling, 1 reply; 20+ messages in thread
From: Reinert Korsnes @ 2001-09-06  9:36 UTC (permalink / raw)


Ted Dennison wrote:

> In article <9n2ctk$36v$1@snipp.uninett.no>, Reinert Korsnes says...
>>
>>Jacob Sparre Andersen wrote:
>>> Seriously, what are you trying to do?
>>
>>Just do a bit different things in the function depending on the type
>>of X.   Say, X is Float of Interger....
> 
> But you can't specify such a parameter, unless you are working with
> generics (is that it?).
> 
> Again, what exactly are you trying to do? Or, is this entirely
> theorietical?

Well, I run into some logic problems which I actually solved
by something -simplified - like (overloading):

generic 
  type T is (<>);
  with function F(X : T) return Float;
procedure P(A : some type; B : another type; X : T);

function F(X: some anumeration type) returns Float;
function F(X: another enumeration type) returns Float;

and:

procedure P1 is new P(T => simething,F);

Someone called John McCabe gave me the hint (in this interesting
discussion).  Thanks...

reinert

PS: Yes, call me "newbie" :-)



> 
> ---
> T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
>           home email - mailto:dennison@telepath.com
> 

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



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

* Re: (elementary question) Test on type ? Pragma inline(granularity)?
  2001-09-06  9:36           ` Reinert Korsnes
@ 2001-09-06 17:10             ` Warren W. Gay VE3WWG
  0 siblings, 0 replies; 20+ messages in thread
From: Warren W. Gay VE3WWG @ 2001-09-06 17:10 UTC (permalink / raw)


Reinert Korsnes wrote:
> Ted Dennison wrote:
...
> generic
>   type T is (<>);
>   with function F(X : T) return Float;
> procedure P(A : some type; B : another type; X : T);
> 
> function F(X: some anumeration type) returns Float;
> function F(X: another enumeration type) returns Float;
> 
> and:
> 
> procedure P1 is new P(T => simething,F);

Actually, you can take this one step further:

generic
  type T is (<>);
  with function F(X : T) return Float is <>;     -- note the "is <>"
procedure P(A : some type; B : another type; X : T);

With the "is <>" added, the compiler can find the correct
procedure/function without the explicit argument supplied:

procedure P1 is new P(T => simething);

Note that if you have a protected type involved within the
generic, with different entrys by type, then you'll have
an additional problem to solve. For example:

   protected type Protected_Buffer_Type is
      entry Put(Items : U8_Array; Last : out Natural);
      entry Put(Items : I8_Array; Last : out Natural);
      entry Put(Items : I16_Array; Last : out Natural);
      entry Put(Items : I32_Array; Last : out Natural);
      entry Get(Items : out U8_Array; Last : out Positive);
      ...
   private
      ...
   end Protected_Buffer_Type;

Using wrapper procedures for each of the Put entries helps
however:

   procedure Put(Buffer : in out Protected_Buffer_Type; Items : U8_Array);
   procedure Put(Buffer : in out Protected_Buffer_Type; Items : I8_Array);
   procedure Put(Buffer : in out Protected_Buffer_Type; Items : I16_Array);
   procedure Put(Buffer : in out Protected_Buffer_Type; Items : I32_Array);

With the use of the "with procedure ... is <>" trick, I was able
to work around the problem (I'd be interested in a more direct
solution if there was one, BTW). After a pragma inline(), the
effect is the same as calling the Put() entry directly from
within the generic code.  But...

How do I explicitly "pragma inline(Put)" for specific procedures? I do
not want to inline all Put() calls; certainly not the Ada.Text_IO.Put()
calls. Hmmm... does this mean the best granularity is at:

   pragma inline(package.etc.Put)

the package level?  Maybe I need more RTFM.

[if there were prior copies of this post that got out, my apologies..
I am struggling with Netscape 6.1 (grrr), this post was finally 
submitted after 5 failures in 6.1, by the older venerable NS 4.7]
-- 
Warren W. Gay VE3WWG
http://members.home.net/ve3wwg



^ 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