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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With