comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Generic formal type with 'Image
Date: Wed, 6 Jun 2018 11:56:01 -0700 (PDT)
Date: 2018-06-06T11:56:01-07:00	[thread overview]
Message-ID: <0dd44c4f-99b5-426d-ba08-831cbb14a795@googlegroups.com> (raw)
In-Reply-To: <pf8m2r$vj$1@dont-email.me>

On Wednesday, June 6, 2018 at 7:03:24 AM UTC-6, 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?

Yes, there is:

  GENERIC
    Type K is (<>);
  PACKAGE Example IS ...

Will do it, as all parameters of K are discrete, and therefore K has the 'Image attribute. (As of Ada 2012 *only* discrete types have the 'Image attribute.)

> 
> The purpose is to avoid:
> 
> generic
>     type Printable is ... -- What should go here?
>     with function Image (P : Printable) return String is <>;
> package Extended_Generic is ...
> 
> and then have to pass the 'Image attribute as the Image function in all 
> instantiations.

This is the more general construct ans will work on something like:

  TYPE Vector IS ARRAY(Positive RANGE <>) of Integer;
  
  Function Image( Input : Vector ) Return String is
    Function List( Data : Vector := Input ) Return String is
    (case Data'Length is
        when 0 => '',
        when 1 => Integer'Image(Data'First),
        when others => Image(Data(Data'First..Natural'Pred(Data'Last))) &
                          Integer'Image(Data'Last)
    );
  Begin
    Return '(' & List & ')';
  End Image;

  Package X is new Extended_Generic( Vector );

this allows more complex items for your Printable parameter; you could in fact make the parameter "Type Printable(<>) is limited private" for near-maximum coverage. (You wouldn't be able to use it on incomplete types.)

> 
> The closest thing I can think of is (<>) but that won't do for floating 
> point types.

This is because floats aren't discrete.

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

This is true.
What you might do is make a sort of generic signature package and work from there.

GENERIC
  Type Numeric is private;
  Function Image( Input Numeric ) Return String is <>;
  Function "+" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "-" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "/" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "*" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "**"( Left : Numeric; Right Natural ) Return Numeric is <>;
  -- Other operations.
PACKAGE General_Numeric IS ...

Then for your particular case use either a child of this, or a generic accepting it as a parameter.


  parent reply	other threads:[~2018-06-06 18:56 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
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 [this message]
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