From: ytomino <aghia05@gmail.com>
Subject: Re: Generic formal type with 'Image
Date: Wed, 6 Jun 2018 07:18:19 -0700 (PDT)
Date: 2018-06-06T07:18:19-07:00 [thread overview]
Message-ID: <cb547976-230b-4bd4-b606-e5b7d723bba9@googlegroups.com> (raw)
In-Reply-To: <pf8m2r$vj$1@dont-email.me>
On Wednesday, June 6, 2018 at 10:03:24 PM UTC+9, Alejandro R. Mosteo wrote:
> I'm pretty sure the answer is "no", but just in case:
>
> Is there a formal for a generic that serves for any type that has a
> predefined 'Image?
>
> The purpose is to avoid:
>
> generic
> type Printable is ... -- What should go here?
> with function Image (P : Printable) return String is <>;
> package
>
> and then have to pass the 'Image attribute as the Image function in all
> instantiations.
>
> The closest thing I can think of is (<>) but that won't do for floating
> point types.
>
> I understand this is an unusually narrow case (I need a generic for many
> numeric types, both discrete and floating, and this would save me some
> typing -- that I have already spent here anyway.)
>
> With these issues I feel a kind of overlap/missed connection between
> attributes and interfaces.
>
> Thanks,
> Alex.
If AI12-0020-1 (http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0020-1.txt?rev=1.7&raw=N) is adopted, 'Image will be able to be used in anywhere.
As the available but unrecommended solution, there is GNAT's Type_Class attribute.
generic
type Printable is private;
package P is
procedure Print (Item : Printable);
end P;
package body P is
procedure Print (Item : Printable) is
begin
case Printable'Type_Class is
when Type_Class_Floating_Point =>
-- We have to convert from the formal private type to some float type, with some dirty hack.
-- This is the case of using streams:
Printable'Write (temporary-stream, Item);
Set_Index (temporary-stream, 1);
case Printable_Type'Stream_Size is
when Float'Stream_Size =>
Float'Read (temporary-stream, Float_Var);
when Long_Float'Stream_Size =>
Long_Float'Read (temporary-stream, Long_Float_Var);
...
Or, as disappointing answer, we have to write normally.
generic
type Printable is private;
with function Image (P : Printable) return String is <>;
with function "+" (Left, Right : Printable) return Printable is <>;
-- Maybe you need "-", "*", "/", "<", "<=", Zero, One, etc
package
next prev parent reply other threads:[~2018-06-06 14:18 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
2018-06-06 14:18 ` ytomino [this message]
2018-06-06 15:34 ` Jeffrey R. Carter
2018-06-06 18:35 ` Alejandro R. Mosteo
2018-06-07 8:35 ` Marius Amado-Alves
2018-06-07 21:26 ` Randy Brukardt
2018-06-06 18:56 ` Shark8
2018-06-06 20:34 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox