comp.lang.ada
 help / color / mirror / Atom feed
* Trying to grasp Generic
@ 2003-10-07 19:36 CheGueVerra
  2003-10-07 22:30 ` Georg Bauhaus
  2003-10-28 11:16 ` Marius Amado Alves
  0 siblings, 2 replies; 11+ messages in thread
From: CheGueVerra @ 2003-10-07 19:36 UTC (permalink / raw)


Ok, I'm new to ADA and I'm trying to use the Generic capacity of ADA.

(ads)
generic
 maxElement : integer := 10;
 type TCle is private;
package TestGen is
 procedure ShowMaxElement;
 procedure ShowCle(Cle : in out TCle);
end testGen;

(adb)
with Text_io;
 package body TestGen is
 package MyInteger is new Text_io.Integer_io(Num => Integer);

 procedure ShowMaxElement is
 begin
  Text_io.New_Line(5);
  MyInteger.Put(MaxElement);
 end;

 procedure ShowCle(Cle : in out TCle) is
 begin
  Text_io.New_line(10);
  Text_io.Put_line("Entered ShowCle");
 end;
end TestGen;

(ada)
with TestGen;
procedure TestGeneric is

 type TGen is record
  Name : String(1..10);
  Age  : natural;
 end record;

 package GenTest is new TestGen(25, TGen);
 MyGen : TGen;

begin
 GenTest.ShowMaxElement;
 MyGen.Name := "SuperNames";
 MyGen.Age := 30;
 GenTest.ShowCle(MyGen);
end TestGeneric;


Now, what I can't figure out, and I'm sure it's quite simple in fact is That
I want my procedure ShowCle to print on the screen the information it
contains, I've tried Text_io but that doesn't seem to be able to do the
trick...

TIA for all the help, for now I'm still lost in the sea of ADA style and
terms so be gentle


CheGueVerra










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

* Re: Trying to grasp Generic
  2003-10-07 19:36 Trying to grasp Generic CheGueVerra
@ 2003-10-07 22:30 ` Georg Bauhaus
  2003-10-08  2:39   ` CheGueVerra
  2003-10-28 11:16 ` Marius Amado Alves
  1 sibling, 1 reply; 11+ messages in thread
From: Georg Bauhaus @ 2003-10-07 22:30 UTC (permalink / raw)


CheGueVerra <chegueverra@hotmail.com> wrote:
 
: type TGen is record
:  Name : String(1..10);
:  Age  : natural;
: end record;
: 
 ...
: GenTest.ShowCle(MyGen);
: end TestGeneric;


: Now, what I can't figure out, and I'm sure it's quite simple in fact is That
: I want my procedure ShowCle to print on the screen the information it
: contains, I've tried Text_io but that doesn't seem to be able to do the
: trick...

If I understand correctly, you want to see what the MyGen object contains?
The Text_IO subprograms can print strings, and other IO packages provide
subprograms for printing numbers.
So you could print the Name compontent of MyGen, and the Age compontent
of MyGen, using these subprograms. Look for Integer_IO, which will provide
another opportunity to see generics in action.

Or you could write a function for TGen objects which returns a string
that you can print, by using the & operator, together with the .Name string,
and the Natural'Image of the .Age component.

An (unrelated) hint that has been given by Ada users much smarter than I am
is that you can make an assignment to a record in one step, which makes
sure you don't accidentally miss some part of the record:

  MyGen := (Name => "SuperNames", Age => 30);

By the way, Ada is both a name of a lady and of a programming language,
while ADA does in fact not denote a programming language :-)


HTH,
-- Georg



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

* Re: Trying to grasp Generic
  2003-10-07 22:30 ` Georg Bauhaus
@ 2003-10-08  2:39   ` CheGueVerra
  2003-10-08  4:09     ` sk
  2003-10-08  4:39     ` Hyman Rosen
  0 siblings, 2 replies; 11+ messages in thread
From: CheGueVerra @ 2003-10-08  2:39 UTC (permalink / raw)


> If I understand correctly, you want to see what the MyGen object contains?
> The Text_IO subprograms can print strings, and other IO packages provide
> subprograms for printing numbers.
> So you could print the Name compontent of MyGen, and the Age compontent
> of MyGen, using these subprograms. Look for Integer_IO, which will provide
> another opportunity to see generics in action.
>
> Or you could write a function for TGen objects which returns a string
> that you can print, by using the & operator, together with the .Name
string,
> and the Natural'Image of the .Age component.

I think the part that I don't understand is that in C++, I would create a
template that would call a function in a base object Base::ShowCle, and with
some casting be able to get the values in the Object because the Object
knows about the method ShowCle.

While I was tracing in OA, I did notice that the package body when entering
the procedure ShowCle that the parameter of the procedure did have the
values I expected it to have.  I just don't see how I can make it generic so
that if ever I change my Record type that ShowCle will still work and would
work for all the ones I will create after that.

> An (unrelated) hint that has been given by Ada users much smarter than I
am
> is that you can make an assignment to a record in one step, which makes
> sure you don't accidentally miss some part of the record:

>   MyGen := (Name => "SuperNames", Age => 30);

Thanks for the tip

> By the way, Ada is both a name of a lady and of a programming language,
> while ADA does in fact not denote a programming language :-)
Well didn't mean to disrespect either the Lady or the language

> HTH,
It did, Just need to do some more research on this, read, read and read some
more I just hope I can finish this damn thing for next week ....


TFYH

CheGueVerra





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

* Re: Trying to grasp Generic
  2003-10-08  2:39   ` CheGueVerra
@ 2003-10-08  4:09     ` sk
  2003-10-08  5:29       ` Jeffrey Carter
  2003-10-08 16:11       ` Stephen Leake
  2003-10-08  4:39     ` Hyman Rosen
  1 sibling, 2 replies; 11+ messages in thread
From: sk @ 2003-10-08  4:09 UTC (permalink / raw)
  To: comp.lang.ada

CheGueVerra <chegueverra@hotmail.com>:

 > ... <snip> ...

You have kind of got things backwards. The display
function has to "know" the fields of the object that
is to be displayed.

A generic function does not "know" the details of any
record types it is instantiated with.

So you can instantiate "TestGen" with "TCLe" set to
any type you choose (within the bounds of the ARM
and the "type <xxx> is private" parameter). However,
the package "TextGen" cannot access or manipulate
the fields of the "TCLe" type.

 > ... I just don't see how I can make it generic so that
 > if ever I change my Record type that ShowCle will still
 > work and would work for all the ones I will create after
 > that.

You can't. Any display function is tightly bound to the
type it is displaying. Trying to create a "generic" to
display all possibilities for type "TCle" is unimaginable.

-- 
-------------------------------------------------
-- Merge vertically for real address
--
--     s n p @ t . o
--      k i e k c c m
-------------------------------------------------




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

* Re: Trying to grasp Generic
  2003-10-08  2:39   ` CheGueVerra
  2003-10-08  4:09     ` sk
@ 2003-10-08  4:39     ` Hyman Rosen
  2003-10-08  9:32       ` CheGueVerra
  1 sibling, 1 reply; 11+ messages in thread
From: Hyman Rosen @ 2003-10-08  4:39 UTC (permalink / raw)


CheGueVerra wrote:
> I think the part that I don't understand is that in C++ ...
> ... and with some casting

If you find yourself using casting in C++, you are almost
certainly doing something wrong.




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

* Re: Trying to grasp Generic
  2003-10-08  4:09     ` sk
@ 2003-10-08  5:29       ` Jeffrey Carter
  2003-10-08 16:11       ` Stephen Leake
  1 sibling, 0 replies; 11+ messages in thread
From: Jeffrey Carter @ 2003-10-08  5:29 UTC (permalink / raw)


Your generic formal parameter is

type TCLe is private;

This means the only things the generic knows about TCLe is that it can 
declare objects of the type, and perform assignment (":="), "=", and 
"/=" on objects of the type. You can instantiate your generic with any 
of the following associations:

TCLe => Integer
TCLe => Float
TCLe => Boolean
TCLe => Character
TCLe => TGen
TCLe => Ada.Strings.Unbounded.Unbounded_String

among many others. Your ShowCle procedure has to work properly for any 
instantiation. How can it possibly do that?

One solution is to add

with function Image (Item : TCLe) return String is <>;

and make ShowCle do

Put_Line (Image (Parameter) );
(Sorry, I don't have your original message to hand and don't recall the 
actual parameter name you used. Parameter is a placeholder for that name.)

Then the code that instantiates your generic has to supply an 
appropriate function to the generic.

-- 
Jeff Carter
"Sheriff murdered, crops burned, stores looted,
people stampeded, and cattle raped."
Blazing Saddles
35




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

* Re: Trying to grasp Generic
  2003-10-08  4:39     ` Hyman Rosen
@ 2003-10-08  9:32       ` CheGueVerra
  0 siblings, 0 replies; 11+ messages in thread
From: CheGueVerra @ 2003-10-08  9:32 UTC (permalink / raw)


I wanted to thank you all for clearing that.

I'm sure that I'll have more questions to come :)

Thanks again
CheGueVerra

"Hyman Rosen" <hyrosen@mail.com> wrote in message
news:KxMgb.36024$541.21807@nwrdny02.gnilink.net...
> CheGueVerra wrote:
> > I think the part that I don't understand is that in C++ ...
> > ... and with some casting
>
> If you find yourself using casting in C++, you are almost
> certainly doing something wrong.
>





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

* Re: Trying to grasp Generic
  2003-10-08  4:09     ` sk
  2003-10-08  5:29       ` Jeffrey Carter
@ 2003-10-08 16:11       ` Stephen Leake
  2003-10-08 16:35         ` CheGueVerra
  1 sibling, 1 reply; 11+ messages in thread
From: Stephen Leake @ 2003-10-08 16:11 UTC (permalink / raw)


sk <noname@myob.com> writes:

> CheGueVerra <chegueverra@hotmail.com>:
> <snip>
>  > ... I just don't see how I can make it generic so that
>  > if ever I change my Record type that ShowCle will still
>  > work and would work for all the ones I will create after
>  > that.
> 
> You can't. Any display function is tightly bound to the
> type it is displaying. Trying to create a "generic" to
> display all possibilities for type "TCle" is unimaginable.

Correct. Which is why I wrote Auto_Text_IO
(http://www.toadmail.com/~ada_wizard/). It reads your record
definition, and writes a Put and Get program for you.

Once you get more used to Ada, give it a try.

-- 
-- Stephe



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

* Re: Trying to grasp Generic
  2003-10-08 16:11       ` Stephen Leake
@ 2003-10-08 16:35         ` CheGueVerra
  2003-10-08 16:41           ` CheGueVerra
  0 siblings, 1 reply; 11+ messages in thread
From: CheGueVerra @ 2003-10-08 16:35 UTC (permalink / raw)


Thanks, though I'm sure I wont be allowed to use it to do the homework I
will look into it and tryto comprehend more how  to talk to the lady...

Thanks
CheGueVerra
> Correct. Which is why I wrote Auto_Text_IO
> (http://www.toadmail.com/~ada_wizard/). It reads your record
> definition, and writes a Put and Get program for you.
>
> Once you get more used to Ada, give it a try.
>
> --
> -- Stephe





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

* Re: Trying to grasp Generic
  2003-10-08 16:35         ` CheGueVerra
@ 2003-10-08 16:41           ` CheGueVerra
  0 siblings, 0 replies; 11+ messages in thread
From: CheGueVerra @ 2003-10-08 16:41 UTC (permalink / raw)


Just realized I already had your site bookmarked :)





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

* Re: Trying to grasp Generic
  2003-10-07 19:36 Trying to grasp Generic CheGueVerra
  2003-10-07 22:30 ` Georg Bauhaus
@ 2003-10-28 11:16 ` Marius Amado Alves
  1 sibling, 0 replies; 11+ messages in thread
From: Marius Amado Alves @ 2003-10-28 11:16 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 2003-10-07 at 19:36, CheGueVerra wrote:
> ...
>  GenTest.ShowMaxElement;
>  MyGen.Name := "SuperNames";
>  MyGen.Age := 30;
>  GenTest.ShowCle(MyGen);
> ...
> Now, what I can't figure out, and I'm sure it's quite simple in fact is That
> I want my procedure ShowCle to print on the screen the information it
> contains, I've tried Text_io but that doesn't seem to be able to do the
> trick...

You mean an output like the following?

  Name: SuperNames
  Age : 30

That would require ShowCle to have knowledge of the record components.
Unfortunately Ada is not reflexive enough for this.
Add the corresponding Show procedure to the generic parameters.




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

end of thread, other threads:[~2003-10-28 11:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-07 19:36 Trying to grasp Generic CheGueVerra
2003-10-07 22:30 ` Georg Bauhaus
2003-10-08  2:39   ` CheGueVerra
2003-10-08  4:09     ` sk
2003-10-08  5:29       ` Jeffrey Carter
2003-10-08 16:11       ` Stephen Leake
2003-10-08 16:35         ` CheGueVerra
2003-10-08 16:41           ` CheGueVerra
2003-10-08  4:39     ` Hyman Rosen
2003-10-08  9:32       ` CheGueVerra
2003-10-28 11:16 ` Marius Amado Alves

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