comp.lang.ada
 help / color / mirror / Atom feed
* passing an array to a generic function
@ 2003-11-21 10:31 Mr. J.
  2003-11-21 11:03 ` Lutz Donnerhacke
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mr. J. @ 2003-11-21 10:31 UTC (permalink / raw)


hi,

this is my generic function:

generic
   type Index is (<>);
   type Item is private;
   type Arr is array(Index range <>) of Item;
   with function "="(I1,I2 : Item) return Boolean is <>;
   
   function Filter_Array(A: Arr) return Arr;

Now, I wanna write a create a Filter_String function according to it:

   type Index is new Integer;
   type Item is new Character;
   type Arr is Array(1..23) of character;
   
   function Filter_String is new Filter_Array(Index,Item,Arr,"=");

As U all experts guessed it won't compile, my question is how to pass
the array of character to my new function ?

10x,
J.



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

* Re: passing an array to a generic function
  2003-11-21 10:31 passing an array to a generic function Mr. J.
@ 2003-11-21 11:03 ` Lutz Donnerhacke
  2003-11-21 11:20 ` David C. Hoos
  2003-11-21 11:35 ` Jean-Pierre Rosen
  2 siblings, 0 replies; 4+ messages in thread
From: Lutz Donnerhacke @ 2003-11-21 11:03 UTC (permalink / raw)


* Mr. J. wrote:
>    type Index is new Integer;
>    type Item is new Character;
>    type Arr is Array(1..23) of character;

type Arr is Array(1 .. 23) of Item;

Item is different from Character.



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

* Re: passing an array to a generic function
  2003-11-21 10:31 passing an array to a generic function Mr. J.
  2003-11-21 11:03 ` Lutz Donnerhacke
@ 2003-11-21 11:20 ` David C. Hoos
  2003-11-21 11:35 ` Jean-Pierre Rosen
  2 siblings, 0 replies; 4+ messages in thread
From: David C. Hoos @ 2003-11-21 11:20 UTC (permalink / raw)


You need to understand and act on the messages the
compiler sends you.

Accordingly, the following code will compile:

generic
   type Index is (<>);
   type Item is private;
   type Arr is array(Index) of Item;
   with function "="(I1,I2 : Item) return Boolean is <>;

   function Filter_Array(A: Arr) return Arr;

   type Index is new Integer range 1 .. 23;
   type Item is new Character;
   type Arr is Array(Index) of Item;

   function Filter_String is new Filter_Array(Index,Item,Arr,"=");

However. when you declare types as new "some other type",
you're creating new types that are distinct from
the "some other type."  Therefore, if as the name of
your instantiation implies, you intend to pass objects
of type string to it, you would do better to do this:

generic
   type Index is (<>);
   type Item is private;
   type Arr is array(Index) of Item;
   with function "="(I1,I2 : Item) return Boolean is <>;

   function Filter_Array(A: Arr) return Arr;

   subtype Index is Integer range 1 .. 23;
   subtype Arr is String (Index);

   function Filter_String is new Filter_Array(Index, Character, Arr, "=");

"Mr. J." <ratsonjaniv@hotmail.com> wrote in message
news:fc116fae.0311210231.d4a9515@posting.google.com...
> hi,
>
> this is my generic function:
>
> generic
>    type Index is (<>);
>    type Item is private;
>    type Arr is array(Index range <>) of Item;
>    with function "="(I1,I2 : Item) return Boolean is <>;
>
>    function Filter_Array(A: Arr) return Arr;
>
> Now, I wanna write a create a Filter_String function according to it:
>
>    type Index is new Integer;
>    type Item is new Character;
>    type Arr is Array(1..23) of character;
>
>    function Filter_String is new Filter_Array(Index,Item,Arr,"=");
>
> As U all experts guessed it won't compile, my question is how to pass
> the array of character to my new function ?
>
> 10x,
> J.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>
>




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

* Re: passing an array to a generic function
  2003-11-21 10:31 passing an array to a generic function Mr. J.
  2003-11-21 11:03 ` Lutz Donnerhacke
  2003-11-21 11:20 ` David C. Hoos
@ 2003-11-21 11:35 ` Jean-Pierre Rosen
  2 siblings, 0 replies; 4+ messages in thread
From: Jean-Pierre Rosen @ 2003-11-21 11:35 UTC (permalink / raw)


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


"Mr. J." <ratsonjaniv@hotmail.com> a �crit dans le message news:
fc116fae.0311210231.d4a9515@posting.google.com...
>    type Index is new Integer;
>    type Item is new Character;
>    type Arr is Array(1..23) of character;
>
>    function Filter_String is new Filter_Array(Index,Item,Arr,"=");
>
> As U all experts guessed it won't compile, my question is how to pass
> the array of character to my new function ?
>
In Ada, all types must match *exactly*. Therefore, you must define Arr as:
type Arr is array (Index range 1..23) of Item;

(1..23 has type Integer, not Index. And Item is a type different from
Character).

--
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





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

end of thread, other threads:[~2003-11-21 11:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-21 10:31 passing an array to a generic function Mr. J.
2003-11-21 11:03 ` Lutz Donnerhacke
2003-11-21 11:20 ` David C. Hoos
2003-11-21 11:35 ` Jean-Pierre Rosen

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