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;
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.
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.
> 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.
> > 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( [...] );
> "Stephen Leake" <stephen_le...@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.
Georg Bauhaus <rm.dash-bauh...@futureapps.de> writes:
> On 01.09.12 00:37, Randy Brukardt wrote:
>> "Stephen Leake" <stephen_le...@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.
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.