comp.lang.ada
 help / color / mirror / Atom feed
* Newbie Ada
@ 2004-04-30 12:17 Axel Druesnes
  2004-04-30 12:22 ` Preben Randhol
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Axel Druesnes @ 2004-04-30 12:17 UTC (permalink / raw)


Hi there,

  i am currently writing a generic package. Despite being really generic 
one (at least) particular type need a special handling in two functions. 
I am therefore wondering how i can either detect the type... So far i 
thought of calling an overloaded function with a variable of the generic 
type as a parameter which would return a special identifier for this 
type but Ada doesn't allow me to call the said function as it consider 
the generic type to be a type of itself instead of a placeholder for the 
real type before the generic package is specialized.

Any suggestion about how i could call a function with the generic type 
as a parameter without creating errors ? Other ways to detect types is 
welcolme ( i can't use an enumeration value in place of a type in this 
case )

ex:

function Type_Id ( Value : in Boolean ) return E_Type_Id;
function Type_Id ( Value : in Integer ) return E_Type_Id;

-- Inside the body
procedure I_Need_The_Type ( Value : Generic_Type )
...
Type_Identifier := Type_Id ( Value ); -- i wish it worked here

Axel Druesnes




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

* Re: Newbie Ada
  2004-04-30 12:17 Newbie Ada Axel Druesnes
@ 2004-04-30 12:22 ` Preben Randhol
  2004-04-30 13:20 ` Hyman Rosen
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Preben Randhol @ 2004-04-30 12:22 UTC (permalink / raw)


On 2004-04-30, Axel Druesnes <druesnes.remove@noos.fr> wrote:
> Hi there,
>
>   i am currently writing a generic package. Despite being really generic 
> one (at least) particular type need a special handling in two functions. 

Then they don't belong in a generic package. I mean if some
procedure/function isn't generic, then why put it in the generic package
in the first place.


-- 
Preben Randhol -------- http://www.pvv.org/~randhol/

()  "Violence is the last refuge of the incompetent"
/\                                   - Isaac Asimov



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

* Re: Newbie Ada
  2004-04-30 12:17 Newbie Ada Axel Druesnes
  2004-04-30 12:22 ` Preben Randhol
@ 2004-04-30 13:20 ` Hyman Rosen
  2004-04-30 13:54 ` Type detection Björn Persson
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Hyman Rosen @ 2004-04-30 13:20 UTC (permalink / raw)


Axel Druesnes wrote:
>  i am currently writing a generic package. Despite being really generic 
> one (at least) particular type need a special handling in two functions. 
> I am therefore wondering how i can either detect the type.

Well, I don't really know Ada, but I think the Ada approach would be to
declare those two functions as generic parameters of the package. Then
when you instantiate, pass in the correct version of the functions along
with the corresponding type.

Conceivably you can also make the types of interest tagged types, and use
inheritance and dispatching to call the correct versions.



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

* Type detection
  2004-04-30 12:17 Newbie Ada Axel Druesnes
  2004-04-30 12:22 ` Preben Randhol
  2004-04-30 13:20 ` Hyman Rosen
@ 2004-04-30 13:54 ` Björn Persson
  2004-04-30 15:04   ` Marius Amado Alves
  2004-05-01  0:28   ` Robert I. Eachus
  2004-04-30 14:49 ` Newbie Ada Lutz Donnerhacke
  2004-05-02 13:33 ` Stephen Leake
  4 siblings, 2 replies; 12+ messages in thread
From: Björn Persson @ 2004-04-30 13:54 UTC (permalink / raw)


Axel Druesnes wrote:

>  i am currently writing a generic package. Despite being really generic 
> one (at least) particular type need a special handling in two functions. 
> I am therefore wondering how i can either detect the type... 

[...]

> function Type_Id ( Value : in Boolean ) return E_Type_Id;
> function Type_Id ( Value : in Integer ) return E_Type_Id;
> 
> -- Inside the body
> procedure I_Need_The_Type ( Value : Generic_Type )
> ....
> Type_Identifier := Type_Id ( Value ); -- i wish it worked here

For the special case of distinguishing integer types from enumeration 
types, I've been thinking of using something like this:

    if Ada.Characters.Handling.Is_Letter
         (Generic_Type'Image(Generic_Type'First)(1)) then
       -- It's an enumeration.
    else
       -- It's an integer type.
    end if;

Further tests could be done to see if an enumeration is Boolean or 
something else.

For more general cases, since the type is known when you instantiate the 
generic package, maybe you can pass an extra parameter (of type 
E_Type_Id)? Or you could keep the common parts in one highly generic 
package and then have a number of more specific generic packages for 
different types. The more specific packages would then instantiate the 
highly generic package and call the common subprograms in it.

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Newbie Ada
  2004-04-30 12:17 Newbie Ada Axel Druesnes
                   ` (2 preceding siblings ...)
  2004-04-30 13:54 ` Type detection Björn Persson
@ 2004-04-30 14:49 ` Lutz Donnerhacke
  2004-05-02 13:33 ` Stephen Leake
  4 siblings, 0 replies; 12+ messages in thread
From: Lutz Donnerhacke @ 2004-04-30 14:49 UTC (permalink / raw)


* Axel Druesnes wrote:
> Any suggestion about how i could call a function with the generic type as
> a parameter without creating errors ? Other ways to detect types is
> welcolme ( i can't use an enumeration value in place of a type in this
> case )
>
> function Type_Id ( Value : in Boolean ) return E_Type_Id;
> function Type_Id ( Value : in Integer ) return E_Type_Id;

You don't need this.

> -- Inside the body
> procedure I_Need_The_Type ( Value : Generic_Type )

First variant: You like to declare and define this function internally.

generic
   type Generic_Type(<>) is limited private;
package XXX is
   procedure I_Need_The_Type (Value : Generic_Type);
end XXX;

package body XXX is
   procedure I_Need_The_Type (Value : Generic_Type) is
   begin
      null;
   end I_Need_The_Type;
end XXX;

Second variant: You need to call an external function of this type.

generic
   type Generic_Type(<>) is limited private;
   with procedure I_Need_The_Type (Value : Generic_Type);
package XXX is
   procedure Doit (x : Generic_Type);
end XXX;

package body XXX is
   procedure Doit (x : Generic_Type) is
   begin
      I_Need_The_Type(x);
   end Doit;
end XXX;

Have fun.



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

* Re: Type detection
  2004-04-30 13:54 ` Type detection Björn Persson
@ 2004-04-30 15:04   ` Marius Amado Alves
  2004-05-01  0:28   ` Robert I. Eachus
  1 sibling, 0 replies; 12+ messages in thread
From: Marius Amado Alves @ 2004-04-30 15:04 UTC (permalink / raw)
  To: comp.lang.ada

Stop! You very very rarely will need to "detect a type" in Ada. The language 
does that for you. Rethink your design. State your problem here clearly, and 
we'll help.



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

* Re: Type detection
  2004-04-30 13:54 ` Type detection Björn Persson
  2004-04-30 15:04   ` Marius Amado Alves
@ 2004-05-01  0:28   ` Robert I. Eachus
  2004-05-01  5:01     ` James Rogers
  2004-05-01 12:42     ` Björn Persson
  1 sibling, 2 replies; 12+ messages in thread
From: Robert I. Eachus @ 2004-05-01  0:28 UTC (permalink / raw)


Bj�rn Persson wrote:

> For the special case of distinguishing integer types from enumeration 
> types, I've been thinking of using something like this...

I extended the idea a little further, but there is still some work to 
do.  A character type is an enumeration type where at least one of the 
enumeration values is a character literal.  So the test for enumeration 
types could be extended with a loop.

Since a boolean type is either Boolean or a type derived from it, I 
guess checking that 'Size = 1 and that the literals are TRUE and FALSE 
should be close enough.

-------------------------------------------------------------------------

with Ada.Text_IO;
with Ada.Characters.Handling;
procedure Datatype is

   generic
     type Unknown is (<>);
   function Find_Datatype return String;

   function Find_Datatype return String is
   begin
     if Ada.Characters.Handling.Is_Letter
             (Unknown'Image(Unknown'First)(1))
     then return "Enumeration";
     elsif Unknown'Pos( Unknown'Base'First) < 0 then return "Integer";
     elsif Unknown'Pos(Unknown'Base'First) = 0 then
       begin
         if Unknown'Succ(Unknown'Last) = Unknown'First
         then return "Unsigned";
         else return "Enumeration?";
         end if;
       exception
         when others => return "Enumeration too";
         -- It's an enumeration.
       end;
     else return "Unknown Type";
     end if;
   end Find_Datatype;

   type Unsigned_16 is mod 2**16;
   type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday);

   function Find_Boolean is new Find_Datatype(Boolean);
   function Find_Integer is new Find_Datatype(Integer);
   function Find_Character is new Find_Datatype(Character);
   function Find_Unsigned is new Find_Datatype(Unsigned_16);
   function Find_Enumeration is new Find_Datatype(Weekday);

begin

   Ada.Text_IO.Put_Line(" Find_Boolean returned " & Find_Boolean);
   Ada.Text_IO.Put_Line(" Find_Integer returned " & Find_Integer);
   Ada.Text_IO.Put_Line(" Find_Character returned " & Find_Character);
   Ada.Text_IO.Put_Line(" Find_Unsigned returned " & Find_Unsigned);
   Ada.Text_IO.Put_Line(" Find_Enumeration returned " & Find_Enumeration);

end Datatype;


-- 

                                           Robert I. Eachus

"The terrorist enemy holds no territory, defends no population, is 
unconstrained by rules of warfare, and respects no law of morality. Such 
an enemy cannot be deterred, contained, appeased or negotiated with. It 
can only be destroyed--and that, ladies and gentlemen, is the business 
at hand."  -- Dick Cheney




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

* Re: Type detection
  2004-05-01  0:28   ` Robert I. Eachus
@ 2004-05-01  5:01     ` James Rogers
  2004-05-01  7:12       ` Robert I. Eachus
  2004-05-01 12:43       ` Björn Persson
  2004-05-01 12:42     ` Björn Persson
  1 sibling, 2 replies; 12+ messages in thread
From: James Rogers @ 2004-05-01  5:01 UTC (permalink / raw)


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

"Robert I. Eachus" <rieachus@comcast.net> wrote in
news:QoydncxqQenacg_d4p2dnA@comcast.com: 

> Bj�rn Persson wrote:
> 
>> For the special case of distinguishing integer types from enumeration
>> types, I've been thinking of using something like this...
> 
> I extended the idea a little further, but there is still some work to 
> do.  A character type is an enumeration type where at least one of the
> enumeration values is a character literal.  So the test for
> enumeration types could be extended with a loop.

The problem with all these tests is an assumption that there is one
integer type. None of these tests can distinguish from various 
integer types. They are only approximately good for determining 
whether a type is an enumeration type or has a value within the
range of Standard.Integer. In Ada this is not even close to type
identification. This is merely distinguishing between enumeration
and non-enumeration discrete types.

Jim Rogers



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

* Re: Type detection
  2004-05-01  5:01     ` James Rogers
@ 2004-05-01  7:12       ` Robert I. Eachus
  2004-05-01 12:43       ` Björn Persson
  1 sibling, 0 replies; 12+ messages in thread
From: Robert I. Eachus @ 2004-05-01  7:12 UTC (permalink / raw)


James Rogers wrote:

> The problem with all these tests is an assumption that there is one
> integer type. None of these tests can distinguish from various 
> integer types. They are only approximately good for determining 
> whether a type is an enumeration type or has a value within the
> range of Standard.Integer. In Ada this is not even close to type
> identification. This is merely distinguishing between enumeration
> and non-enumeration discrete types.

No, the tests are independent of the number of integer types, 'Pos 
returns a universal_integer value, so all the comparisons use that type. 
If somehow you had an enumeration type with more values than 
Standard.Integer'Last, the code would still work fine.

As to your other point, I thought that was the object, to determine 
which (sub)CLASS of types a generic formal belonged to, when the generic 
used (<>);

-- 

                                           Robert I. Eachus

"The terrorist enemy holds no territory, defends no population, is 
unconstrained by rules of warfare, and respects no law of morality. Such 
an enemy cannot be deterred, contained, appeased or negotiated with. It 
can only be destroyed--and that, ladies and gentlemen, is the business 
at hand."  -- Dick Cheney




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

* Re: Type detection
  2004-05-01  0:28   ` Robert I. Eachus
  2004-05-01  5:01     ` James Rogers
@ 2004-05-01 12:42     ` Björn Persson
  1 sibling, 0 replies; 12+ messages in thread
From: Björn Persson @ 2004-05-01 12:42 UTC (permalink / raw)


Robert I. Eachus wrote:

> Björn Persson wrote:
> 
>> For the special case of distinguishing integer types from enumeration 
>> types, I've been thinking of using something like this...
> 
> I extended the idea a little further, [...]

With these additions:

>   type Unsigned_16 is mod 2**16;
   subtype Usub is Unsigned_16 range 3 .. 12;
   subtype Csub is Character range 'b' .. 'c';
>   type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday);
> 
>   function Find_Boolean is new Find_Datatype(Boolean);
>   function Find_Integer is new Find_Datatype(Integer);
>   function Find_Character is new Find_Datatype(Character);
   function Find_Csub is new Find_Datatype(Csub);
>   function Find_Unsigned is new Find_Datatype(Unsigned_16);
   function Find_Usub is new Find_Datatype(Usub);
>   function Find_Enumeration is new Find_Datatype(Weekday);
> 
> begin
> 
>   Ada.Text_IO.Put_Line(" Find_Boolean returned " & Find_Boolean);
>   Ada.Text_IO.Put_Line(" Find_Integer returned " & Find_Integer);
>   Ada.Text_IO.Put_Line(" Find_Character returned " & Find_Character);
   Ada.Text_IO.Put_Line(" Find_Csub returned " & Find_Csub);
>   Ada.Text_IO.Put_Line(" Find_Unsigned returned " & Find_Unsigned);
   Ada.Text_IO.Put_Line(" Find_Usub returned " & Find_Usub);
>   Ada.Text_IO.Put_Line(" Find_Enumeration returned " & Find_Enumeration);

I get this output:

  Find_Boolean returned Enumeration
  Find_Integer returned Integer
  Find_Character returned Enumeration
  Find_Csub returned Enumeration?
  Find_Unsigned returned Unsigned
  Find_Usub returned Enumeration?
  Find_Enumeration returned Enumeration

I think using 'Base here too would be better:

 >         if Unknown'Succ(Unknown'Last) = Unknown'First
         if Unknown'Succ(Unknown'Base'Last) = Unknown'Base'First

Then I get:

  Find_Boolean returned Enumeration
  Find_Integer returned Integer
  Find_Character returned Enumeration
  Find_Csub returned Enumeration too
  Find_Unsigned returned Unsigned
  Find_Usub returned Unsigned
  Find_Enumeration returned Enumeration

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Type detection
  2004-05-01  5:01     ` James Rogers
  2004-05-01  7:12       ` Robert I. Eachus
@ 2004-05-01 12:43       ` Björn Persson
  1 sibling, 0 replies; 12+ messages in thread
From: Björn Persson @ 2004-05-01 12:43 UTC (permalink / raw)


James Rogers wrote:

> This is merely distinguishing between enumeration
> and non-enumeration discrete types.

Which is exactly what I think I will need. Whether it's enough for Axel 
Druesnes I don't know.

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Newbie Ada
  2004-04-30 12:17 Newbie Ada Axel Druesnes
                   ` (3 preceding siblings ...)
  2004-04-30 14:49 ` Newbie Ada Lutz Donnerhacke
@ 2004-05-02 13:33 ` Stephen Leake
  4 siblings, 0 replies; 12+ messages in thread
From: Stephen Leake @ 2004-05-02 13:33 UTC (permalink / raw)
  To: comp.lang.ada

Axel Druesnes <druesnes.remove@noos.fr> writes:

> Hi there,
> 
>   i am currently writing a generic package. Despite being really
> generic one (at least) particular type need a special handling in two
> functions. I am therefore wondering how i can either detect the
> type... 

You probably don't really need to detect the type.

Since you are new to Ada, there is probably a better way to do what
you realy need to do.

Please tell us at a higher level what you are trying to do, so we can
tell you that better way.

-- 
-- Stephe




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

end of thread, other threads:[~2004-05-02 13:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-30 12:17 Newbie Ada Axel Druesnes
2004-04-30 12:22 ` Preben Randhol
2004-04-30 13:20 ` Hyman Rosen
2004-04-30 13:54 ` Type detection Björn Persson
2004-04-30 15:04   ` Marius Amado Alves
2004-05-01  0:28   ` Robert I. Eachus
2004-05-01  5:01     ` James Rogers
2004-05-01  7:12       ` Robert I. Eachus
2004-05-01 12:43       ` Björn Persson
2004-05-01 12:42     ` Björn Persson
2004-04-30 14:49 ` Newbie Ada Lutz Donnerhacke
2004-05-02 13:33 ` Stephen Leake

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