开发者

Creating Ada record with one field

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜