* What's wrong with my code?
@ 2008-04-28 14:42 amal.alphonse
2008-04-28 15:18 ` stefan-lucks
` (5 more replies)
0 siblings, 6 replies; 10+ messages in thread
From: amal.alphonse @ 2008-04-28 14:42 UTC (permalink / raw)
Sorry if I'm posting in the wrong forum.
I have a generic package, with specification:
generic
type Element is private;
with procedure Element_Put(E : in Element);
package SelectionP is
type My_Array is array (0..10) of Element;
Max : constant Integer := 10;
procedure Find_Min (A : My_Array; Offset : Integer; Pos : Integer);
procedure Swap (A : in out My_Array; First : Integer; Second :
Integer);
procedure Sort (A : in out My_Array);
procedure Print (A : in My_Array);
end SelectionP;
--------------------------------------------------
and its package body is:
package body SelectionP is
A : My_Array;
procedure Find_Min (A : My_Array; Offset : Integer; Pos : Integer)
is
...
end Find_Min;
procedure Swap (A : in out My_Array; First : Integer; Second :
Integer) is
...
end Swap;
procedure Sort (A : in out My_Array) is
...
end Sort;
procedure Print (A: in out My_Array) is
...
end Print;
end SelectionP;
-------------------------------------------
And I try to make use of this package in this file:
with Ada.Text_IO, Ada.Integer_Text_IO;
with SelectionP;
procedure SelectionPUser is
package IntSelectionP is new SelectionP(Integer, Ada.Text_IO.Put);
begin
Ada.Text_IO.New_Line;
end SelectionPUser;
---
I try to compile but it says 'no visible subprogram matches the
specification for Element_Put' referring to the line above where I
make the new package IntSelectionP. I can't see what i've done wrong.
Also, is my code correct if my purpose is to use the package to create
arrays of different elements (integer, character, etc) and use the
procedures Find_Min and Sort and Print on them?
Thanks
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
@ 2008-04-28 15:18 ` stefan-lucks
2008-04-28 15:22 ` stefan-lucks
2008-04-28 15:23 ` george.priv
` (4 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: stefan-lucks @ 2008-04-28 15:18 UTC (permalink / raw)
On Mon, 28 Apr 2008, amal.alphonse@gmail.com wrote:
> Sorry if I'm posting in the wrong forum.
You are perfectly right here.
> with Ada.Text_IO, Ada.Integer_Text_IO;
> with SelectionP;
> procedure SelectionPUser is
Replace the following line
> package IntSelectionP is new SelectionP(Integer, Ada.Text_IO.Put);
by
procedure Put_Int(I: Integer) is
begin
Ada.Integer_Text_IO.Put(I);
end Put_Int;
package Int_SelectionP is new X(Integer, Put_Int);
The specification of Put in Ada.Integer_Text_IO is
procedure Put(Item : in Num;
Width : in Field := Default_Width;
Base : in Number_Base := Default_Base);
Thanks to the default parameters, you can just write
"Ada.Integer_Text_IO.Put(I);" to *call* this procedure Put. But using the
same Put as a generic parameter appears to be different ...
> begin
> Ada.Text_IO.New_Line;
> end SelectionPUser;
> procedure Print (A: in out My_Array) is
Nitpick: this should be
procedure Print(A in My_Array) is
to conform with the specification.
Additional point: Most Ada programmers depreciate identifiers like
IntSelectionP, SelectionP, ... but prefer to use underscores:
Int_Selection_P, Selection_P. This improves readability and copes well
with the case-insensitivity of Ada identifiers. (E.g., for ada Selections
and SelectionS are the same, but Selections and Selection_S are different.
I currently can't find a better example, but I hope you get the idea.)
> Also, is my code correct if my purpose is to use the package to create
> arrays of different elements (integer, character, etc) and use the
> procedures Find_Min and Sort and Print on them?
I guess you are going to compare your elements? Then you'll want to
specify another generic parameter (in addition to the type Element and the
procedure Element_Put), such as
function "<"(Left, Right: Element) return Boolean;
--
------ Stefan Lucks -- Bauhaus-University Weimar -- Germany ------
Stefan dot Lucks at uni minus weimar dot de
------ I love the taste of Cryptanalysis in the morning! ------
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 15:18 ` stefan-lucks
@ 2008-04-28 15:22 ` stefan-lucks
2008-04-28 15:24 ` stefan-lucks
0 siblings, 1 reply; 10+ messages in thread
From: stefan-lucks @ 2008-04-28 15:22 UTC (permalink / raw)
> Additional point: Most Ada programmers depreciate identifiers like
> IntSelectionP, SelectionP, ... but prefer to use underscores:
> Int_Selection_P, Selection_P. This improves readability and copes well
> with the case-insensitivity of Ada identifiers. (E.g., for ada Selections
> and SelectionS are the same, but Selections and Selection_S are different.
> I currently can't find a better example, but I hope you get the idea.)
Perhaps a better example: IntSelectionP and IntsElectionP are the same
identifyer, but Ints_Election_P is different.
--
------ Stefan Lucks -- Bauhaus-University Weimar -- Germany ------
Stefan dot Lucks at uni minus weimar dot de
------ I love the taste of Cryptanalysis in the morning! ------
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
2008-04-28 15:18 ` stefan-lucks
@ 2008-04-28 15:23 ` george.priv
2008-04-28 16:52 ` Ivan Levashew
` (3 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: george.priv @ 2008-04-28 15:23 UTC (permalink / raw)
On Apr 28, 10:42 am, amal.alpho...@gmail.com wrote:
> Sorry if I'm posting in the wrong forum.
>
> I have a generic package, with specification:
>
> generic
> type Element is private;
> with procedure Element_Put(E : in Element);
> package SelectionP is
> type My_Array is array (0..10) of Element;
> Max : constant Integer := 10;
> procedure Find_Min (A : My_Array; Offset : Integer; Pos : Integer);
> procedure Swap (A : in out My_Array; First : Integer; Second :
> Integer);
> procedure Sort (A : in out My_Array);
> procedure Print (A : in My_Array);
> end SelectionP;
> --------------------------------------------------
> and its package body is:
>
> package body SelectionP is
>
> A : My_Array;
>
> procedure Find_Min (A : My_Array; Offset : Integer; Pos : Integer)
> is
> ...
> end Find_Min;
>
> procedure Swap (A : in out My_Array; First : Integer; Second :
> Integer) is
> ...
> end Swap;
>
> procedure Sort (A : in out My_Array) is
> ...
> end Sort;
> procedure Print (A: in out My_Array) is
> ...
> end Print;
>
> end SelectionP;
> -------------------------------------------
>
> And I try to make use of this package in this file:
> with Ada.Text_IO, Ada.Integer_Text_IO;
> with SelectionP;
> procedure SelectionPUser is
> package IntSelectionP is new SelectionP(Integer, Ada.Text_IO.Put);
should be:
package IntSelectionP is new SelectionP(Integer,
Ada.Integer_Text_IO.Put);
> begin
> Ada.Text_IO.New_Line;
> end SelectionPUser;
>
> ---
>
> I try to compile but it says 'no visible subprogram matches the
> specification for Element_Put' referring to the line above where I
> make the new package IntSelectionP. I can't see what i've done wrong.
>
> Also, is my code correct if my purpose is to use the package to create
> arrays of different elements (integer, character, etc) and use the
> procedures Find_Min and Sort and Print on them?
For these types you should be fine. For more complex types you may
need to define "<" and "=" operators.
George.
>
> Thanks
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 15:22 ` stefan-lucks
@ 2008-04-28 15:24 ` stefan-lucks
0 siblings, 0 replies; 10+ messages in thread
From: stefan-lucks @ 2008-04-28 15:24 UTC (permalink / raw)
On Mon, 28 Apr 2008, stefan-lucks@see-the.signature wrote:
> Perhaps a better example: IntSelectionP and IntsElectionP are the same
> identifyer, but Ints_Election_P is different.
I mean different from Int_Selection_P.
I hope I am done with the typos, now.
--
------ Stefan Lucks -- Bauhaus-University Weimar -- Germany ------
Stefan dot Lucks at uni minus weimar dot de
------ I love the taste of Cryptanalysis in the morning! ------
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
2008-04-28 15:18 ` stefan-lucks
2008-04-28 15:23 ` george.priv
@ 2008-04-28 16:52 ` Ivan Levashew
2008-04-29 9:18 ` Ludovic Brenta
2008-04-29 5:30 ` christoph.grein
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Ivan Levashew @ 2008-04-28 16:52 UTC (permalink / raw)
amal.alphonse@gmail.com пишет:
> Sorry if I'm posting in the wrong forum.
IRC & pastebin
--
If you want to get to the top, you have to start at the bottom
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
` (2 preceding siblings ...)
2008-04-28 16:52 ` Ivan Levashew
@ 2008-04-29 5:30 ` christoph.grein
2008-04-29 9:31 ` Ludovic Brenta
2008-04-29 10:33 ` amal.alphonse
5 siblings, 0 replies; 10+ messages in thread
From: christoph.grein @ 2008-04-29 5:30 UTC (permalink / raw)
Integer as an index is too big. Why not:
type Index is range 0..10;
subtype Index is Integer range 0..10; -- alternatively
type My_Array is array (Index) of Element;
procedure Find_Min (A: My_Array; Offset, Pos: Index);
procedure Swap (A: in out My_Array; First, Second: Index);
etc
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 16:52 ` Ivan Levashew
@ 2008-04-29 9:18 ` Ludovic Brenta
0 siblings, 0 replies; 10+ messages in thread
From: Ludovic Brenta @ 2008-04-29 9:18 UTC (permalink / raw)
Ivan Levashew wrote:
> amal.alphonse@gmail.com пишет:
> > Sorry if I'm posting in the wrong forum.
> IRC & pastebin
No, IMHO comp.lang.ada is much better as it is asynchronous and
archived.
--
Ludovic Brenta.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
` (3 preceding siblings ...)
2008-04-29 5:30 ` christoph.grein
@ 2008-04-29 9:31 ` Ludovic Brenta
2008-04-29 10:33 ` amal.alphonse
5 siblings, 0 replies; 10+ messages in thread
From: Ludovic Brenta @ 2008-04-29 9:31 UTC (permalink / raw)
> generic
> type Element is private;
> with procedure Element_Put(E : in Element);
> package SelectionP is
Others have explained why neither Ada.Text_IO.Put nor
Ada.Integer_Text_IO.Put are acceptable actuals for Element_Put.
Element_Put is only necessary for one subprogram (procedure Print).
Yet, all users of your package must provide an actual for Element_Put
even if they don't call Print. Similarly, Find_Min and Sort are likely
to require a function "<" to compare Elements. So I suggest:
generic
type Element is private;
package Selection_P is
type Index_Type is range 0 .. 10;
type My_Array is array (Index_Type) of Element;
generic
with function "<" (Left, Right : in Element) return Boolean is
<>;
procedure Find_Min (A : My_Array; Offset : in Index_Type; Pos : out
Index_Type);
procedure Swap (A : in out My_Array; First, Second : Index_Type);
generic
with function "<" (Left, Right : in Element) return Boolean is
<>;
procedure Sort (A : in out My_Array);
generic
with procedure Put (E : in Element) is <>;
procedure Put (A : in My_Array);
end Selection_P;
The reason I didn't place "<" as a generic formal parameter of the
package, but rather of Find_Min and Sort, is because you might want to
find the maximum element but sort in ascending order, like this:
package S is new Selection_P (Element => Integer);
procedure Find_Max is new S.Find_Min ("<" => ">"); -- use ">" as the
actual
procedure Sort is new S.Sort; -- use the default "<", sort in
ascending order
procedure Put (J : in Integer) is
begin
Ada.Integer_Text_IO.Put (J);
end Put;
procedure Put is new S.Put; -- use the default
HTH
--
Ludovic Brenta.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: What's wrong with my code?
2008-04-28 14:42 What's wrong with my code? amal.alphonse
` (4 preceding siblings ...)
2008-04-29 9:31 ` Ludovic Brenta
@ 2008-04-29 10:33 ` amal.alphonse
5 siblings, 0 replies; 10+ messages in thread
From: amal.alphonse @ 2008-04-29 10:33 UTC (permalink / raw)
On 28 Apr, 15:42, amal.alpho...@gmail.com wrote:
> Sorry if I'm posting in the wrong forum.
>
...
> Thanks
Thanks for the posts. I will amend my program to work. Also, I don't
need to define a comparison operation "<," I was just trying to make a
simple selection sort.
Thanks again for the help!
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-04-29 10:33 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-28 14:42 What's wrong with my code? amal.alphonse
2008-04-28 15:18 ` stefan-lucks
2008-04-28 15:22 ` stefan-lucks
2008-04-28 15:24 ` stefan-lucks
2008-04-28 15:23 ` george.priv
2008-04-28 16:52 ` Ivan Levashew
2008-04-29 9:18 ` Ludovic Brenta
2008-04-29 5:30 ` christoph.grein
2008-04-29 9:31 ` Ludovic Brenta
2008-04-29 10:33 ` amal.alphonse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox