From: stefan-lucks on
On Thu, 6 May 2010, Warren wrote:

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

How about overloading the &-operator:

function "&"(S: String; Item: T) return String;

if X is of type T and you "use type T", you can replace the expression

T'Image(X)

by

"" & X

If your T'Image(X) is part of a larger string expression, overloading &
appears to be very convenient (for the "typical lazy programmer", as you
put it):

Ada.Text_IO.Put_Line("The first result is " & X &
" the second result is " & Y & ".");

So long

Stefan


--
------ Stefan Lucks -- Bauhaus-University Weimar -- Germany ------
Stefan dot Lucks at uni minus weimar dot de
------ I love the taste of Cryptanalysis in the morning! ------

From: Dmitry A. Kazakov on
On Thu, 6 May 2010 21:20:46 -0500, Randy Brukardt wrote:

> "Dmitry A. Kazakov" <mailbox(a)dmitry-kazakov.de> wrote in message
> news:1ojboulapml8w$.1w5gfpk45kh72.dlg(a)40tude.net...
>> On Thu, 06 May 2010 16:22:03 -0400, Robert A Duff wrote:
>>
>>> Warren <ve3wwg(a)gmail.com> writes:
>>>
>>>> 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));
>>>
>>> I don't understand that. You need to chase down the package
>>> in which T is declared, which is the same package in which
>>> To_String (or better, Image) is declared.
>>
>> I always declare I/O/formatting stuff in a child package. So, T'Image has
>> some minor advantages. (The argument would really work if it were V'Image
>> or V.Image)
>
> Except that you can't specify an attribute of a type after it is frozen. So
> if you could specify T'Image, you couldn't define the Image function that
> you specified in a child package. Thus Bob is right: the type and the
> function has to be in the same package.

Right. Primitive operations are somewhat incompatible with modularity (and
multiple dispatch as well). There must be something wrong with that, though
I cannot figure out what.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: J-P. Rosen on
Robert A Duff a �crit :
> "Yannick Duch�ne (Hibou57)" <yannick_duchene(a)yahoo.fr> writes:
> But for "columns" you want something like:
>
> 1
> -3
> 123
> -123
>
> But 'Image gives you:
>
> 1
> -3
> 123
> -123
>
> I don't get it. 'Image isn't particularly helpful in producing
> columnar output.
>

'Image is just for quick, debug-like output. If you want nice formatted
output, by all means use the IO packages, that's what they are for.

--
---------------------------------------------------------
J-P. Rosen (rosen(a)adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
From: Georg Bauhaus on
On 5/6/10 7:10 PM, Warren wrote:

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

Apologies for the length of the following. In short:
not in this manner, use Ada instead, don't tweak
the language, no need.

[1. mislead assumptions].
If, as Bob has said, programmers discover 'Image before
they discover Text_IO.*_IO and Text_IO.Editing, then there
is something wrong, indeed---with Ada's appearance before
them! If you want them to be able to write an output
line quickly, give them a package! Don't misuse 'Image.
They may be quite used to industry standard logging packages
and such, no hurdle there.

[2. 'Image *and* 'Value are paired and have a purpose,
or contract].
Is 'Image a formatting function? I don't think so.
Is it intended to be one? I can't imagine that, not in any
non-trivial sense of the word "formatting".
'Value is its counter part. Is it a scanning function?
I don't think that, either, for the same reason.
And if 'Image and 'Value come in pairs, redefining
one without the other seems wrong. So what should a user-
defined 'Value be? Shouldn't it agree with what the LRM
implies? Should not the new Generic_Dispatching_Constructor
be its equivalent?
Should we, in spite of all this, deprive ourselves of
what we routinely do in other cases? That is, ideally we
express the purpose of a function call with suitably and
freely chosen names for an implementation that meets our
needs.

You said you were too lazy to ... and therefore wanted
to adapt something half related to your purpose.
You might be doing yourself (and your readers) a disservice.
To see this: What is the contract if 'Image, expressed
as pre- and post-conditions? What could this description
be in the standard if programmers we invited to dismiss
printing packages in favor of user defined 'Image, thereby
manipulating the contract of 'Image?

[3. Use normal Ada for polymorphism].
If I wanted polymorphic behavior of numbers,
I'd make them have a suitable type. Or wrap them in
one where necessary, e.g. during I/O. Or mix them with
a package that provides "polymorphic appearance" like
Dmitry's or, IIUC, PragmAda.

You can say

type Derived_Num_Type is new <some number type>;

overriding
function Image (X: Derived_Num_Type) return String;

where Derived_Num_Type is both tag-less and has an
Image operation that makes strings the way you want
them.

[4. no need].
Why do Ada programmers forget Ada language
principles whenever it comes to basic values? :o(
Is it not obvious that using 'Image in ways it was not
intended to be used makes the program text misleading?

I can understand that the customer is always right
and that Ada vendors would quite naturally consider
tweaking 'Image. But I also hope they have ways to
provide alternatives based on existing Ada that will
serve both their customers and Ada. Certainly you know
what your image functions should do. So write them.


-- Georg


From: Stephen Leake on
"Dmitry A. Kazakov" <mailbox(a)dmitry-kazakov.de> writes:

> On Thu, 06 May 2010 16:22:03 -0400, Robert A Duff wrote:
>
>> Warren <ve3wwg(a)gmail.com> writes:
>>
>>> 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));
>>
>> I don't understand that. You need to chase down the package
>> in which T is declared, which is the same package in which
>> To_String (or better, Image) is declared.
>
> I always declare I/O/formatting stuff in a child package.

Me, to. And the childe package is named Images.

> So, T'Image has some minor advantages. (The argument would really work
> if it were V'Image or V.Image)

It's Foo.Bar.T'Image vs Foo.Bar.Images.Image. No problem.

--
-- Stephe