comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: anonymous records as tuples
Date: Mon, 12 Mar 2018 19:01:24 +0100
Date: 2018-03-12T19:01:24+01:00	[thread overview]
Message-ID: <p86f9k$1nio$1@gioia.aioe.org> (raw)
In-Reply-To: 4cf2a76e-626d-4ead-ae8a-dccdef41b283@googlegroups.com

On 2018-03-12 18:32, Stephen Leake wrote:

> We could do something similar in Ada with an "anonymous record":
> 
> declare
>     constant record
>       Real : Integer;
>       Virtual : Integer;
>     end record := Tree.Count_Terminals (Index);
> begin
>     if Virtual = 0 then
>        Next_Shared_Token := Next_Shared_Token - Real;
>     end if;
> end;
> 
> Perhaps the 'constant' should appear on each component instead; then some could be not constant.

By analogy to anonymous arrays:

  Temp : constant record
                     Real    : Integer;
                     Virtual : Integer;
                  end record := Tree.Count_Terminals (Index);

> Count_Terminals would be declared to return an anonymous record:

> function Count_Terminals (Tree : in out Tree_Type; Index : in Index_Type)
> return record
>    Real : Integer;
>    Virtual : Integer;
> end record;

This has no analogy yet. If anonymous records were allowed, so must be 
arrays:

    function Read return array (Positive range <>) of Character;

BTW, this and anonymous record parameters are analogous [polymorphism] 
to abstract array/record interface. You return a "class-wide" type which 
matches any concrete type (instance) with the same structure.

I prefer explicit classes to by-structure anonymous types as more 
organized and controlled way to handle what is in the class and what is not.

> The body of Count_Terminals would return a record aggregate for the result.
> 
> Allowing anonymous records in other contexts could easily get messy.

It requires matching by-structure, which is always messy.

> Or we could require Record_1 to contain an anonymous record:
> 
> type Record_1 is record
>     A : Float;
>     record
      ^^^^
      C : record

Anonymous record type /= anonymous object/component.

>        Real : Integer;
>        Virtual : Integer;
>     end record;
>     B : Float;
> end record;
> 
> I have no idea how complicated this would be for compiler implementors.
> 
> I searched the Ada Issues database (http://ada-auth.org/search-ai12s.html) for "anonymous record"; it occurs once (http://www.ada-auth.org/sresult.cgi?Search=23&Index=21&File=292&Seq=1), where Bob Duff complains that Ada has anonymous arrays but not anonymous records.
> 
> "tuples" appears many times, mostly in discussions, never in an actual proposal.

Tuples are bit more than just anonymous record types. Tuples also 
require a method of flattening an aggregate or list of arguments/results 
and promotion to a record.

For example when you want pass a record and then pass it down to a 
subprogram as an argument list. Or when you want to declare all elements 
of the result tuple. So that instead of writing this:

    declare
       <anonymous> : record
          Real    : Integer;
          Virtual : Integer;
       end record := Count_Terminals (Tree, Index);
       Real    : Integer renames <anonymous>.Real;
       Virtual : Integer renames <anonymous>.Virtual;

You will do:

    declare
       <> : Count_Terminals (Tree, Index);

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


  reply	other threads:[~2018-03-12 18:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-12 17:32 anonymous records as tuples Stephen Leake
2018-03-12 18:01 ` Dmitry A. Kazakov [this message]
2018-03-12 19:17 ` Robert Eachus
2018-03-12 19:34   ` Stephen Leake
2018-03-12 23:59     ` Randy Brukardt
2018-03-13  2:51       ` Dan'l Miller
2018-03-13  8:28         ` Dmitry A. Kazakov
2018-03-12 21:09 ` Jeffrey R. Carter
replies disabled

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