From: Stephen Leake on
"Jeffrey R. Carter" <ggsub(a)pragmada.x10hosting.com> writes:

> On Apr 20, 12:25 am, Stephen Leake <stephen_le...(a)stephe-leake.org>
> wrote:
>>
>> generic
>>    Width : Natural;
>>    type Number_Type is mod <>;
>> function SAL.Generic_Hex_Image (Item : in Number_Type) return String;
>> --  Return a hexadecimal image of Item, padded with leading zeros to
>> --  Width. If Width is too small for Item, leading digits are silently
>> --  truncated.
>
> So if I sometimes want different widths for the same type, I have to
> have multiple instantiations?

Yes. But in practice, the width you want is Number_Type'Width (but
assuming hexadecimal), so it's not a problem.

The only problem I've encountered is the "silently truncated" part. We
changed a number type to have more bits, but forgot to change the Width
parameter here.

There are times, such as building formatted dates, when I need a
specific number of characters, and want an exception if the actual value
can't be represented in that number. Then this function is not
appropriate.

> That doesn't seem very friendly to me. I don't see why Width couldn't
> be a parameter of the function,

It could; in the body, it is only used as the size of the result image
string. It's just historical accident that it is a generic parameter
instead.

> probably defaulted to Number_Type'Width.

That assumes base 10, so it would be wrong. If I made a change like
this, I'd keep the generic parameter as the default width, or add a
function that computes a default width assuming base 16, and use that as
the default.

> What do I do for signed integer images,

I never need to image signed integers in hex; that's confusing.

> for bases other than 16,

sal-generic_binary_image.ads
sal-generic_decimal_image.ads

are the only ones I've ever needed. The binary image includes '_' every
four digits.

> and for signed integer images for bases other than 16?

Never needed them.

> Do I have to roll my own for these cases?

Yes.

It is far easier to test a small, focused routine than a large, general
purpose one.

> In Ada.Text_IO, a Width of zero implies the minimum width needed to
> represent the value; here it's an expensive way to get a null String.
> I'd prefer consistency with the standard.

If this function were proposed as part of the Ada standard, that would
make sense. However, it is part of SAL, which often has a different
philosophy.

> I haven't used SAL, but these are the kind of comments you'd probably
> get from me if I did use this function. HTH.

Thanks for your comments.

--
-- Stephe