From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: anonymous records as tuples
Date: Mon, 12 Mar 2018 10:32:44 -0700 (PDT)
Date: 2018-03-12T10:32:44-07:00 [thread overview]
Message-ID: <4cf2a76e-626d-4ead-ae8a-dccdef41b283@googlegroups.com> (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
next reply other threads:[~2018-03-12 17:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-12 17:32 Stephen Leake [this message]
2018-03-12 18:01 ` anonymous records as tuples 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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox