Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Ada record with one field

Tags:

ada

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?

like image 346
ada hater Avatar asked Apr 30 '10 08:04

ada hater


1 Answers

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.

like image 127
Lohrun Avatar answered Oct 10 '22 10:10

Lohrun