From: Warren on
Jeffrey R. Carter expounded in news:hrv32p$nnu$1(a)tornado.tornevall.net:

> Warren wrote:
>>
>> type LNumber_Type is range 0..99_999;
>>
>> Can I declare..
>>
>> for LNumber_Type'Image use LNumber_To_String;
>
> No. Nor can I see why you'd want to. Once you declare
>
> function Image (Value : Lnumber_Type) return String;
>
> why would you want to write
>
> Lnumber_Type'Image (I)
>
> rather than
>
> Image (I)
>
> ?

I agree that it is indeed clumsier. It's just that
I tend to use it a lot in debug output, rather than
chasing down the package prefix for the To_String()
function. I know I can always get away with:

Put_Line("The value V=" & T'Image(V));

But everyone has convinced me that it is not a good
practice, even if it were allowed. :)

Warren
From: Warren on
Adam Beneschan expounded in news:2318cabb-080c-42a0-8219-21c347abe172
@o11g2000yqj.googlegroups.com:

>> �type LNumber_Type is range 0..99_999;
>>
>> Can I declare..
>>
>> �for LNumber_Type'Image use LNumber_To_String;
>
> No.
>
> Broadly, you can't use a FOR attribute specification on every
> attribute, just a few select ones that the language specifically says
> you can. 'Image isn't one of those.
>
> I think it's been mentioned a few times on this newsgroup that it
> might be nice to have this ability, but I don't see that anyone has
> submitted an actual language change proposal.
>
> -- Adam

There's probably bigger fish to fry, I suppose.

I'm just being a typical lazy programmer (not always
wanting to lookup the correct package prefix for
the To_String() function to use).

Warren
From: Warren on
=?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= expounded in
news:op.vcaqajn0ule2fv(a)garhos:

> Le Thu, 06 May 2010 19:10:20 +0200, Warren <ve3wwg(a)gmail.com> a
> écrit:
>
>> I have tried to google for this and have not yet found
>> a suitable answer, so I'll troll, er, poll for an
>> answer here..
>>
>> Is there the ability to substitute your own S'Image
>> function? For example, a basic interpreter might
>> define:
>>
>> type LNumber_Type is range 0..99_999;
>>
>> Can I declare..
>>
>> for LNumber_Type'Image use LNumber_To_String;

> However, it later says:
>
> [ARM 2005 13.3 (5/1)]
> An attribute_designator is allowed in an attribute_definition_clause
> only if this International Standard explicitly allows it
....
> Finally, it should be checked if Image is explicitely allowed as an
> attribute designator for an operational clause (I may check later to
> give you a more formal answer to this one question).

As others have suggested, probably not, or at best on
an implementation basis only. At best, this suggests
that this approach is unportable.

>> Finally, there is actually a third question- more along
>> the lines of "Should this language feature be used
>> in this manner?", or is it preferable to just code your
>> own along the lines of (which is what I presently use):
>>
>> function To_String(LNO : LNumber_Type) return String;

> I would say, it is more handy to define a function, because a function
> would be able to hold the exact formatting parameter your application
> requires, it would be able to hold the exact optional defaults for
> those parameters and it would better integrates with the overall
> general design of an Ada application, that, “withing” package and
> using renames clause. Using attribute, you are require to always use
> thye type name as a prefix for such things as 'Image.

True, but in my case I just needed the leading zeros, sans
any sign:

900 to be displayed as "00900" (vs " 900").

> All of this providing I've understood what you were requesting for.

Yep, you got it.

Warren
From: Warren on
Dmitry A. Kazakov expounded in news:10yarz51f24i9$.k5ouloeombf2$.dlg@
40tude.net:

> On Thu, 6 May 2010 17:10:20 +0000 (UTC), Warren wrote:
>
>> I have tried to google for this and have not yet found
>> a suitable answer, so I'll troll, er, poll for an
>> answer here..
>>
>> Is there the ability to substitute your own S'Image
>> function? For example, a basic interpreter might
>> define:
>>
>> type LNumber_Type is range 0..99_999;
>>
>> Can I declare..
>>
>> for LNumber_Type'Image use LNumber_To_String;
>>
>> If so, then the question is what the signature of
>> the S'Image function looks like-- is it:
>>
>> function LNumber_To_String(LNO : LNumber_Type) return String;
>
> I am not sure what do you mean, but the following is legal Ada:
>
> generic
> type T is private;
> with function Image (X : T) return String;
> package P is
> ...
> end P;
>
> type LNumber_Type is range 0..99_999;
>
> package PI is new P (LNumber_Type, LNumber_Type'Image);
>
> So attribute is a "plain" function with a clumsy name.

I would never have though along those lines, but agree
this isn't the "Right Thing"(T).

Thanks
From: Robert A Duff on
Warren <ve3wwg(a)gmail.com> writes:

> As others have suggested, probably not, or at best on
> an implementation basis only. At best, this suggests
> that this approach is unportable.

Definitely not, and not on an implementation-defined basis.
An implementation may define its own attributes, and it
can choose to make those user-specifiable, or not.
But an implementation cannot make 'Image user-specifiable.

Of course, an implementation can have a non-standard mode in
which 'Image is user-specifiable. But in that mode, it's not
an Ada implementation.

> True, but in my case I just needed the leading zeros, sans
> any sign:
>
> 900 to be displayed as "00900" (vs " 900").

So create a function called Image that does that.
I think Image is a better name than To_String, by the way
-- it's a common convention to use Image.

The leading blank produced by T'Image is indeed annoying!
What were they thinking?! Never mind, that's been discussed
to death. It's not a huge problem, but it's one of the
first things new Ada programmers notice, and it's a real
turn-off.

- Bob