comp.lang.ada
 help / color / mirror / Atom feed
* anonymous records as tuples
@ 2018-03-12 17:32 Stephen Leake
  2018-03-12 18:01 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Stephen Leake @ 2018-03-12 17:32 UTC (permalink / raw)


I recently had to change a function that returned a single integer into a procedure that returns two. So my code when from something like:

Next_Shared_Token := Next_Shared_Token - Tree.Count_Terminals (Index);

to:

declare
   Real : Integer;
   Virtual : Integer;
begin
   Tree.Count_Terminals (Index, Real, Virtual);
   if Virtual = 0 then
      Next_Shared_Token := Next_Shared_Token - Real;
   end if;
end;

This bothers me mainly because the variables Real, Virtual are not initialized, and not declared constant, when clearly they should be.

Some other languages handle a similar situation with tuples. For example, in Lua (https://www.lua.org/manual/5.3/manual.html#3.33) this would be:

Real, Virtual = Tree.Count_Terminals (Tree_Index);

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.

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;

The body of Count_Terminals would return a record aggregate for the result.

Allowing anonymous records in other contexts could easily get messy. For example, there could be a larger record containing Real, Virtual:

type Record_1 is record
   A : Float;
   Real : Integer;
   Virtual : Integer;
   B : Float;
end record;

Then an aggregate for that could be:

(A => 1.0, 
 record Real, Virtual end record => Tree.Count_Terminals (I), 
 B => 2.0)

Alternately, the function result could be implicitly deconstructed:

(1.0, Count_Terminals (I), 2.0)

Or we could require Record_1 to contain an anonymous record:

type Record_1 is record
   A : Float;
   record
      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.

-- Stephe

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

end of thread, other threads:[~2018-03-13  8:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-12 17:32 anonymous records as tuples Stephen Leake
2018-03-12 18:01 ` Dmitry A. Kazakov
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

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