tags:

views:

76

answers:

1

I've define a type:

type Foo is record
   bar : Positive;
end record;

I want to create a function that returns an instance of the record:

function get_foo return Foo is
    return (1);
end get_foo;

But Ada won't let me, saying "positional aggregate cannot have one argument".
Stupidly trying, I've added another dumb field to the record, and then return (1, DOESNT_MATTER); works!

How do I tell Ada that's not a positional aggregate, but an attempt to create a record?

+3  A: 

The positional aggregate initialization cannot be used with record having only one component, but that does not mean you cannot have record with one component.

The values of a record type are specified by giving a list of named fields. The correct code for your get_foo function should be as following.

function get_foo return Foo is
    return (bar => 1);
end get_foo;

You can also specify the type of the record using the Foo'(bar => 1) expression.

Using the list of named components is better in practice than positional initilization. You can forget the position of the component and it does not change if you add a new field into your record.

Lohrun
Thank you! you made me hate Ada a bit less! :)
ada hater
That's the trick.
T.E.D.