comp.lang.ada
 help / color / mirror / Atom feed
* creating own Image function
@ 2004-12-29 16:51 R
  2004-12-29 17:01 ` Vinzent 'Gadget' Hoefler
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: R @ 2004-12-29 16:51 UTC (permalink / raw)


Hello.

I've written my own Image function it's sth like Java's toString.

function Image(this: rec1) return String is
tmp : String(1..26);
begin
tmp := "The value of field is:" & Integer'Image(Get(this));
return tmp;
end Image;

when I use it:

testclass.Create(object, 100); -- note 100 is 3 digits = 3 Characters
Put(testclass.Image(object));

then I can see "The value of field is: 100"
when I initiate my object with 10 - 2 digits = 2 Characters
an exception is raised:
CONSTRAINT_ERROR : testclass.adb:34 length check failed

it's because tmp: Sting has it's fixed length can it be more flexible?

When I tried with String(1..100) the same effect
users can initate their objects with 1, 11, 111, 111111 and so on.

so my question is: can I write(with Your help of course) a function
flexible
to handle all those situations?

thanks in advnce
best regards R




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

* Re: creating own Image function
  2004-12-29 16:51 creating own Image function R
@ 2004-12-29 17:01 ` Vinzent 'Gadget' Hoefler
  2004-12-29 17:04 ` Mark Lorenzen
  2004-12-29 17:06 ` Bobby D. Bryant
  2 siblings, 0 replies; 11+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-12-29 17:01 UTC (permalink / raw)


R wrote:

> I've written my own Image function it's sth like Java's toString.
> 
> function Image(this: rec1) return String is
> tmp : String(1..26);
> begin
> tmp := "The value of field is:" & Integer'Image(Get(this));
> return tmp;
> end Image;

|function Image (This : Rec1) return String is
|begin
|   return "The value of field is:" & Integer'Image(Get(This));
|end Image;

"tmp" is unnecessary here. And: If you *really* need strings with
non-fixed size, use Unbounded_String.


Vinzent.



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

* Re: creating own Image function
  2004-12-29 16:51 creating own Image function R
  2004-12-29 17:01 ` Vinzent 'Gadget' Hoefler
@ 2004-12-29 17:04 ` Mark Lorenzen
  2004-12-29 17:20   ` Luke A. Guest
  2004-12-29 17:06 ` Bobby D. Bryant
  2 siblings, 1 reply; 11+ messages in thread
From: Mark Lorenzen @ 2004-12-29 17:04 UTC (permalink / raw)


"R" <ruthless@poczta.onet.pl> writes:

> Hello.
> 
> I've written my own Image function it's sth like Java's toString.
> 
> function Image(this: rec1) return String is
> tmp : String(1..26);
> begin
> tmp := "The value of field is:" & Integer'Image(Get(this));
> return tmp;
> end Image;

Your questions are really newbee questions and I recommend that you
read a book about Ada in order to get it right from the beginning and
not take your knowledge of other languages into "Ada world".

> 
> when I use it:
> 
> testclass.Create(object, 100); -- note 100 is 3 digits = 3 Characters
> Put(testclass.Image(object));
> 
> then I can see "The value of field is: 100"
> when I initiate my object with 10 - 2 digits = 2 Characters
> an exception is raised:
> CONSTRAINT_ERROR : testclass.adb:34 length check failed
> 
> it's because tmp: Sting has it's fixed length can it be more flexible?
> 
> When I tried with String(1..100) the same effect
> users can initate their objects with 1, 11, 111, 111111 and so on.
> 
> so my question is: can I write(with Your help of course) a function
> flexible
> to handle all those situations?

Try:

function Image(this: rec1) return String is
begin
  return "The value of field is:" & Integer'Image(Get(this));
end Image;

> 
> thanks in advnce
> best regards R

- Mark Lorenzen



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

* Re: creating own Image function
  2004-12-29 16:51 creating own Image function R
  2004-12-29 17:01 ` Vinzent 'Gadget' Hoefler
  2004-12-29 17:04 ` Mark Lorenzen
@ 2004-12-29 17:06 ` Bobby D. Bryant
  2004-12-29 17:46   ` Georg Bauhaus
  2 siblings, 1 reply; 11+ messages in thread
From: Bobby D. Bryant @ 2004-12-29 17:06 UTC (permalink / raw)


On Wed, 29 Dec 2004, "R" <ruthless@poczta.onet.pl> wrote:

> Hello.
> 
> I've written my own Image function it's sth like Java's toString.
> 
> function Image(this: rec1) return String is
> tmp : String(1..26);
> begin
> tmp := "The value of field is:" & Integer'Image(Get(this));
> return tmp;
> end Image;
> 
> when I use it:
> 
> testclass.Create(object, 100); -- note 100 is 3 digits = 3 Characters
> Put(testclass.Image(object));
> 
> then I can see "The value of field is: 100"
> when I initiate my object with 10 - 2 digits = 2 Characters
> an exception is raised:
> CONSTRAINT_ERROR : testclass.adb:34 length check failed
> 
> it's because tmp: Sting has it's fixed length can it be more flexible?
> 
> When I tried with String(1..100) the same effect
> users can initate their objects with 1, 11, 111, 111111 and so on.
> 
> so my question is: can I write(with Your help of course) a function
> flexible to handle all those situations?


I think you can declare tmp as -

   tmp : String := "The value of field is:" & Integer'Image(Get(this));


For that matter, I think you can skip the declaration and just do -

   return "The value of field is:" & Integer'Image(Get(this));

as the body of your function.


-- 
Bobby Bryant
Austin, Texas



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

* Re: creating own Image function
  2004-12-29 17:04 ` Mark Lorenzen
@ 2004-12-29 17:20   ` Luke A. Guest
  2004-12-29 20:56     ` Mark Lorenzen
  2004-12-30  4:02     ` Jeffrey Carter
  0 siblings, 2 replies; 11+ messages in thread
From: Luke A. Guest @ 2004-12-29 17:20 UTC (permalink / raw)


On Wed, 29 Dec 2004 18:04:08 +0100, Mark Lorenzen wrote:

> "R" <ruthless@poczta.onet.pl> writes:
> 
>> Hello.
>> 
>> I've written my own Image function it's sth like Java's toString.
>> 
>> function Image(this: rec1) return String is
>> tmp : String(1..26);
>> begin
>> tmp := "The value of field is:" & Integer'Image(Get(this));
>> return tmp;
>> end Image;
> 
> Your questions are really newbee questions and I recommend that you
> read a book about Ada in order to get it right from the beginning and
> not take your knowledge of other languages into "Ada world".

Hmmm, a bit too harsh, me thinks. Obviously, just correcting it like you
did would've been enough.

Although, he does have a good point (kind of), about creating your own
image functions, I would prefer it if you could provide an Image function
for a type and have T'Image call that function. That would be a really
nice addition to the language.

Luke.




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

* Re: creating own Image function
  2004-12-29 17:06 ` Bobby D. Bryant
@ 2004-12-29 17:46   ` Georg Bauhaus
  0 siblings, 0 replies; 11+ messages in thread
From: Georg Bauhaus @ 2004-12-29 17:46 UTC (permalink / raw)


Bobby D. Bryant <bdbryant@mail.utexas.edu> wrote:
 
: I think you can declare tmp as -
: 
:   tmp : String := "The value of field is:" & Integer'Image(Get(this));

or,

    tmp : constant String :=
                    "The value of field is:" & Integer'Image(Get(this));
 

: For that matter, I think you can skip the declaration and just do -
: 
:   return "The value of field is:" & Integer'Image(Get(this));
: 
: as the body of your function.



-- Georg



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

* Re: creating own Image function
  2004-12-29 17:20   ` Luke A. Guest
@ 2004-12-29 20:56     ` Mark Lorenzen
  2004-12-30  4:04       ` Jeffrey Carter
  2004-12-30  4:02     ` Jeffrey Carter
  1 sibling, 1 reply; 11+ messages in thread
From: Mark Lorenzen @ 2004-12-29 20:56 UTC (permalink / raw)


"Luke A. Guest" <laguest@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk> writes:

> On Wed, 29 Dec 2004 18:04:08 +0100, Mark Lorenzen wrote:
> 
> > "R" <ruthless@poczta.onet.pl> writes:
> > 
> >> Hello.
> >> 
> >> I've written my own Image function it's sth like Java's toString.
> >> 
> >> function Image(this: rec1) return String is
> >> tmp : String(1..26);
> >> begin
> >> tmp := "The value of field is:" & Integer'Image(Get(this));
> >> return tmp;
> >> end Image;
> > 
> > Your questions are really newbee questions and I recommend that you
> > read a book about Ada in order to get it right from the beginning and
> > not take your knowledge of other languages into "Ada world".
> 
> Hmmm, a bit too harsh, me thinks. Obviously, just correcting it like you
> did would've been enough.

It wasn't my intention to be harsh. I just think that trying to
program small examples and then post them to c.l.a in order to get
them corrected, is not the best way to learn Ada or other programming
languages. Just like there is a C++ way to do things in C++ there is
an Ada way to do things in Ada. It is important to learn the Ada way
of thinking in order to really use the language in an effective
way. Otherwise the OP will just try to write C++ programs in Ada and
think that Ada sucks because it is darn hard (and ugly) to write C++
programs in Ada.

> 
> Although, he does have a good point (kind of), about creating your own
> image functions, I would prefer it if you could provide an Image function
> for a type and have T'Image call that function. That would be a really
> nice addition to the language.
> 
> Luke.

- Mark Lorenzen



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

* Re: creating own Image function
  2004-12-29 17:20   ` Luke A. Guest
  2004-12-29 20:56     ` Mark Lorenzen
@ 2004-12-30  4:02     ` Jeffrey Carter
  2005-01-15 23:58       ` Sandro Magi
  1 sibling, 1 reply; 11+ messages in thread
From: Jeffrey Carter @ 2004-12-30  4:02 UTC (permalink / raw)


Luke A. Guest wrote:
> On Wed, 29 Dec 2004 18:04:08 +0100, Mark Lorenzen wrote:
> 
>>Your questions are really newbee questions and I recommend that you
>>read a book about Ada in order to get it right from the beginning and
>>not take your knowledge of other languages into "Ada world".
> 
> Hmmm, a bit too harsh, me thinks. Obviously, just correcting it like you
> did would've been enough.

I disagree. These are mostly extremely basic questions that the OP would 
not be asking if he'd looked at any text or worked through any tutorial. 
Given the number of texts and tutorials available on line through 
adapower.com and adaworld.com, I don't think it's a good idea to answer 
these questions, as it only encourages more. While I'm all for 
encouraging people to learn Ada, I'm not in favor of teaching it through 
posts and responses on c.l.a. I'm perfectly willing to answer questions 
from someone who's made the effort to learn the basics.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



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

* Re: creating own Image function
  2004-12-29 20:56     ` Mark Lorenzen
@ 2004-12-30  4:04       ` Jeffrey Carter
  2004-12-30  8:32         ` Vinzent 'Gadget' Hoefler
  0 siblings, 1 reply; 11+ messages in thread
From: Jeffrey Carter @ 2004-12-30  4:04 UTC (permalink / raw)


Mark Lorenzen wrote:

> it is darn hard (and ugly) to write C++
> programs in Ada.

As it should be. Now if it were only darn hard and ugly to write them in 
C++ ...

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



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

* Re: creating own Image function
  2004-12-30  4:04       ` Jeffrey Carter
@ 2004-12-30  8:32         ` Vinzent 'Gadget' Hoefler
  0 siblings, 0 replies; 11+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-12-30  8:32 UTC (permalink / raw)


Jeffrey Carter wrote:

> As it should be. Now if it were only darn hard and ugly to write them
> in C++ ...

Oh. In some way, it is. ;)


Vinzent.



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

* Re: creating own Image function
  2004-12-30  4:02     ` Jeffrey Carter
@ 2005-01-15 23:58       ` Sandro Magi
  0 siblings, 0 replies; 11+ messages in thread
From: Sandro Magi @ 2005-01-15 23:58 UTC (permalink / raw)


Jeffrey Carter wrote:

> I disagree. These are mostly extremely basic questions that the OP would
> not be asking if he'd looked at any text or worked through any tutorial.
> Given the number of texts and tutorials available on line through
> adapower.com and adaworld.com, I don't think it's a good idea to answer
> these questions, as it only encourages more. While I'm all for
> encouraging people to learn Ada, I'm not in favor of teaching it through
> posts and responses on c.l.a. I'm perfectly willing to answer questions
> from someone who's made the effort to learn the basics.

I disagree. I worked my way through many tutorials when learning Ada a few
months ago, and nowhere did I find a mention of custom attributes or and
functions like 'Image (I had to ask here). The answer was buried in the
Reference Manual, which someone here helpfully pointed out. The RM is not
something immediately recommended for newbies IMO; it's far too high a
barrier to entry.



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

end of thread, other threads:[~2005-01-15 23:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-29 16:51 creating own Image function R
2004-12-29 17:01 ` Vinzent 'Gadget' Hoefler
2004-12-29 17:04 ` Mark Lorenzen
2004-12-29 17:20   ` Luke A. Guest
2004-12-29 20:56     ` Mark Lorenzen
2004-12-30  4:04       ` Jeffrey Carter
2004-12-30  8:32         ` Vinzent 'Gadget' Hoefler
2004-12-30  4:02     ` Jeffrey Carter
2005-01-15 23:58       ` Sandro Magi
2004-12-29 17:06 ` Bobby D. Bryant
2004-12-29 17:46   ` Georg Bauhaus

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