From: Warren on
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;

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;

Inquiring minds need to know,

Warren
From: Dmitry A. Kazakov on
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.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Adam Beneschan on
On May 6, 10:10 am, Warren <ve3...(a)gmail.com> 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;

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
From: Yannick Duchêne (Hibou57) on
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;
>
> If so, then the question is what the signature of
> the S'Image function looks like-- is it:
As far as I could tell, your intuition is good, as there is indeed such
things in Ada, called either representation clauses or operational
clauses. See [ARM 2005 13.3] for more about it.

An excerpt to be kind:

[ARM 2005 13.3 (2)]
attribute_definition_clause ::=
for local_name'attribute_designator use expression;
| for local_name'attribute_designator use name;


[ARM 2005 13.3 (4)]
For an attribute_definition_clause that specifies an attribute that
denotes a subprogram, the expected profile for the name is the profile
required for the attribute.

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

Comes with a tiny example:


[AARM 2005 13.3 (6a)]
Ramification: This implies, for example, that if one writes:
for T'Read use R;
R has to be a procedure with two parameters with the appropriate subtypes
and modes as shown in

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).

I use representation clauses from time to time, but I've never used this
kind of one, for the reason I give you right after now.

> function LNumber_To_String(LNO : LNumber_Type) return String;
>
> 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;
>
> Inquiring minds need to know,
>
> Warren
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. With function, you may “withed” the package
defining this function, and make it part of the local scope using
something like “function F (...) ... renames My_Package.F ...”.

More handy IMHO.

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

--
No-no, this isn't an oops ...or I hope (TM) - Don't blame me... I'm just
not lucky
From: Jeffrey R. Carter on
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)

?

--
Jeff Carter
"I soiled my armor, I was so scared."
Monty Python & the Holy Grail
71