comp.lang.ada
 help / color / mirror / Atom feed
* anonymous aggregates?
@ 2012-08-31 10:22 Stephen Leake
  2012-08-31 11:06 ` Brian Drummond
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Stephen Leake @ 2012-08-31 10:22 UTC (permalink / raw)


Occasionally I have the following pattern:

declare
    A : Integer;
    B : Float;

    procedure Foo (A : out Integer; B : out Float);
begin
   Foo (A, B);
   ...
end;

where Foo initializes A, B.

In general, I prefer to initialize variables in the declaration, and
declare them constant if possible. But I can't do that here.

One option in current Ada is to declare a record type;

declare
    type Foo_Return_Type is record
        A : Integer;
        B : Float;
    end record;

    function Foo return Foo_Return_Type is
    begin
        return
            (A => 1,    
             B => 2.0);
    end Foo;

    A_B : constant Foo_Return_Type := Foo;
begin
   ...
end;

But that seems like overkill, especially since the code in "..." must be
edited; all references to A must be replaced by A_B.A.

If we introduce the notion of "anonymous aggregates" (styled after
"anonymous arrays"), we could do this:

declare
    function Foo return
     (A : Integer;
      B : Float) 
    is begin
        return
            (A => 1,    
             B => 2.0);
    end Foo;

    (A : constant integer,
     B : constant Float) := Foo;
begin
   ...
end;

I haven't thought much about how this would be implemented.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 10:22 anonymous aggregates? Stephen Leake
@ 2012-08-31 11:06 ` Brian Drummond
  2012-08-31 12:02 ` Dmitry A. Kazakov
  2012-08-31 22:37 ` Randy Brukardt
  2 siblings, 0 replies; 9+ messages in thread
From: Brian Drummond @ 2012-08-31 11:06 UTC (permalink / raw)


On Fri, 31 Aug 2012 06:22:35 -0400, Stephen Leake wrote:

> Occasionally I have the following pattern:
> 
> declare
>     A : Integer;
>     B : Float;
> 
>     procedure Foo (A : out Integer; B : out Float);

...
> In general, I prefer to initialize variables in the declaration, and
> declare them constant if possible. But I can't do that here.
> 
> One option in current Ada is to declare a record type;
> 
> declare
>     type Foo_Return_Type is record
>         A : Integer;
>         B : Float;
>     end record;
...
> But that seems like overkill, especially since the code in "..." must be
> edited; all references to A must be replaced by A_B.A.

All references, or just one?

(in the declarations section, just after the initialisation of A_B)
A : constant Integer renames A_B.A;

- Brian




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 10:22 anonymous aggregates? Stephen Leake
  2012-08-31 11:06 ` Brian Drummond
@ 2012-08-31 12:02 ` Dmitry A. Kazakov
  2012-08-31 15:15   ` Adam Beneschan
  2012-08-31 22:37 ` Randy Brukardt
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2012-08-31 12:02 UTC (permalink / raw)


On Fri, 31 Aug 2012 06:22:35 -0400, Stephen Leake wrote:

> If we introduce the notion of "anonymous aggregates" (styled after
> "anonymous arrays"), we could do this:
> 
> declare
>     function Foo return
>      (A : Integer;
>       B : Float) 
>     is begin
>         return
>             (A => 1,    
>              B => 2.0);
>     end Foo;
> 
>     (A : constant integer,
>      B : constant Float) := Foo;
> begin
>    ...
> end;
> 
> I haven't thought much about how this would be implemented.

There are two separate issues here:

1. anonymous record types, one of the things I liked to have very much as
well as an ability to use anonymous arrays and aggregates as components.

The syntax is obvious:

function Foo return
   record
      A : Integer;
      B : Float;
   end record;

2. Usage of aggregates (of any kind) on the left side of assignment and in
out parameters. One could consider Ada 2012 aliased parameters somehow
extended on record components and array elements.

Another possible mechanism could be argument type conversion as in Ada 83
when a tuple (A, B) explicitly converted to a record as out parameter. This
roughly corresponds to your idea.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 12:02 ` Dmitry A. Kazakov
@ 2012-08-31 15:15   ` Adam Beneschan
  0 siblings, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2012-08-31 15:15 UTC (permalink / raw)


On Friday, August 31, 2012 5:02:03 AM UTC-7, Dmitry A. Kazakov wrote:

> 
> There are two separate issues here:
> 
> 1. anonymous record types, one of the things I liked to have very much as
> well as an ability to use anonymous arrays and aggregates as components.
> 
> The syntax is obvious:
> 
> function Foo return
>    record
>       A : Integer;
>       B : Float;
>    end record;

I seem to recall someone saying, probably on this newsgroup, that the early designers of Ada were keeping open the possibility of anonymous record types.  That's one reason why the syntax ends with "end record" instead of "end <typename>" (just to tie this to a different thread!).  It could be that the original version of the language had anonymous record types somewhere in the syntax, and it got eliminated before Ada 83 was standardized.  But I don't really know.

                               -- Adam




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 10:22 anonymous aggregates? Stephen Leake
  2012-08-31 11:06 ` Brian Drummond
  2012-08-31 12:02 ` Dmitry A. Kazakov
@ 2012-08-31 22:37 ` Randy Brukardt
  2012-08-31 22:57   ` Shark8
  2012-09-01  9:57   ` Georg Bauhaus
  2 siblings, 2 replies; 9+ messages in thread
From: Randy Brukardt @ 2012-08-31 22:37 UTC (permalink / raw)


"Stephen Leake" <stephen_leake@stephe-leake.org> wrote in message 
news:85mx1bwec4.fsf@stephe-leake.org...
...
> If we introduce the notion of "anonymous aggregates" (styled after
> "anonymous arrays"), we could do this:

I'd be more likely to call this idea an "anonymous record" as that is the 
obvious counterpart to "anonymous array".

declare
    function Foo return
       record
          A : Integer;
          B : Float;
        end record
    is begin
        return
            (A => 1,
             B => 2.0);
    end Foo;

(As a side-benefit, there'd be less griping about not repeating the name in 
a record declaration. ;-)

I suspect that this was not done originally in Ada mainly because of 
concerns about arbitrarily introducing additional identifiers in the middle 
of other declarations. Not sure that's a real problem; there might be some 
weird visibility issues that arose.

                                       Randy.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 22:37 ` Randy Brukardt
@ 2012-08-31 22:57   ` Shark8
  2012-09-01  9:57   ` Georg Bauhaus
  1 sibling, 0 replies; 9+ messages in thread
From: Shark8 @ 2012-08-31 22:57 UTC (permalink / raw)


On Friday, August 31, 2012 4:37:58 PM UTC-6, Randy Brukardt wrote:
> "Stephen Leake" <stephen_leake@stephe-leake.org> wrote in message 
> 
> news:85mx1bwec4.fsf@stephe-leake.org...
> ...
> 
> > If we introduce the notion of "anonymous aggregates" (styled after
> > "anonymous arrays"), we could do this:
> 
> I'd be more likely to call this idea an "anonymous record" as that is the 
> obvious counterpart to "anonymous array".
> declare
> 
>     function Foo return
>        record
>           A : Integer;
>           B : Float;
>         end record
>     is begin
>         return
>             (A => 1,
>              B => 2.0);
>     end Foo;
> 
> 
> (As a side-benefit, there'd be less griping about not repeating the name in 
> a record declaration. ;-)
> 
> I suspect that this was not done originally in Ada mainly because of 
> concerns about arbitrarily introducing additional identifiers in the middle 
> of other declarations. Not sure that's a real problem; there might be some 
> weird visibility issues that arose.

The thing I see is that if you needed to do something moderately complicated, where a extended-return must be used... There's nothing that lets you do this, to my knowledge, in the language, so we would need a 'TYPE attribute that would return out the given object's type. (We already got the 'Return in Ada 2012 so we could use it in post-conditions.) Then we could have:

SubType Initializing_Info is Boolean; -- To be changed for later/complex items.
Function Testing( Init : Initializing_Info ) Return
        record
           A : Integer;
           B : Float;
         end record
     is begin
         return Result : Testing'Return'Type:=
             (A => 1,
              B => 2.0) do
             if Init then
                 A := 23;
             end if;
         end return;
     end Testing;

There have been a few times I've come across where being able to use the type like that would be advantageous... but no interesting specific example is coming to mind, but with discretes you could make 'Img a bit portable by writing Discrete_var'Type'Image( Discrete_var ) -- A bit wordy, but it would be quite portable in this case and a renames might make that a bit more friendly -- and using streams would be a bit easier if you have things functional but want to use streams and just fiddle with your record components:
Field_Name'Type'Write( [...] );



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-08-31 22:37 ` Randy Brukardt
  2012-08-31 22:57   ` Shark8
@ 2012-09-01  9:57   ` Georg Bauhaus
  2012-09-02 11:25     ` Stephen Leake
  1 sibling, 1 reply; 9+ messages in thread
From: Georg Bauhaus @ 2012-09-01  9:57 UTC (permalink / raw)


On 01.09.12 00:37, Randy Brukardt wrote:
> "Stephen Leake" <stephen_leake@stephe-leake.org> wrote in message
> news:85mx1bwec4.fsf@stephe-leake.org...
> ...
>> If we introduce the notion of "anonymous aggregates" (styled after
>> "anonymous arrays"), we could do this:
>
> I'd be more likely to call this idea an "anonymous record" as that is the
> obvious counterpart to "anonymous array".
>
> declare
>      function Foo return
>         record
>            A : Integer;
>            B : Float;
>          end record
>      is begin
>          return
>              (A => 1,
>               B => 2.0);
>      end Foo;
>
> (As a side-benefit, there'd be less griping about not repeating the name in
> a record declaration. ;-)

Wouldn't we be passing objects of anonymous_type_1 that right now
cannot be assigned to anything of anonymous_type_2?
At least not without resorting to types being the same if they
happen use the same structure (and component names?), thus giving up
Ada's notion of type equivalence.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-09-01  9:57   ` Georg Bauhaus
@ 2012-09-02 11:25     ` Stephen Leake
  2012-09-02 12:34       ` Dmitry A. Kazakov
  0 siblings, 1 reply; 9+ messages in thread
From: Stephen Leake @ 2012-09-02 11:25 UTC (permalink / raw)


Georg Bauhaus <rm.dash-bauhaus@futureapps.de> writes:

> On 01.09.12 00:37, Randy Brukardt wrote:
>> "Stephen Leake" <stephen_leake@stephe-leake.org> wrote in message
>> news:85mx1bwec4.fsf@stephe-leake.org...
>> ...
>>> If we introduce the notion of "anonymous aggregates" (styled after
>>> "anonymous arrays"), we could do this:
>>
>> I'd be more likely to call this idea an "anonymous record" as that is the
>> obvious counterpart to "anonymous array".
>>
>> declare
>>      function Foo return
>>         record
>>            A : Integer;
>>            B : Float;
>>          end record
>>      is begin
>>          return
>>              (A => 1,
>>               B => 2.0);
>>      end Foo;
>>
>> (As a side-benefit, there'd be less griping about not repeating the name in
>> a record declaration. ;-)
>
> Wouldn't we be passing objects of anonymous_type_1 that right now
> cannot be assigned to anything of anonymous_type_2?
> At least not without resorting to types being the same if they
> happen use the same structure (and component names?), thus giving up
> Ada's notion of type equivalence.

Good point; anonymous arrays are different types, and cannot be assigned
to each other. So anonymous records should be the same, which defeats
the purpose.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: anonymous aggregates?
  2012-09-02 11:25     ` Stephen Leake
@ 2012-09-02 12:34       ` Dmitry A. Kazakov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2012-09-02 12:34 UTC (permalink / raw)


On Sun, 02 Sep 2012 07:25:25 -0400, Stephen Leake wrote:

> Good point; anonymous arrays are different types, and cannot be assigned
> to each other. So anonymous records should be the same, which defeats
> the purpose.

But you didn't want to assign records. Initialization /= assignment.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-09-09  6:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-31 10:22 anonymous aggregates? Stephen Leake
2012-08-31 11:06 ` Brian Drummond
2012-08-31 12:02 ` Dmitry A. Kazakov
2012-08-31 15:15   ` Adam Beneschan
2012-08-31 22:37 ` Randy Brukardt
2012-08-31 22:57   ` Shark8
2012-09-01  9:57   ` Georg Bauhaus
2012-09-02 11:25     ` Stephen Leake
2012-09-02 12:34       ` Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox