Ada: What is the purpose of `limited` in `limited interface`

Viewed 203

I have started learning Ada a few weeks ago. I understand that limited in some contexts declares a limited type, for whom copying of objects is not allowed

From Ada Reference Manual 2012 7.5 1/2

A limited type is (a view of) a type for which copying (such as for an assignment_statement) is not allowed. A nonlimited type is a (view of a) type for which copying is allowed.

However, since an interface type is inherently abstract, an 'interface object' cannot be created, and thus cannot be copied. Is keyword limited redundant here? What's the difference between limited interface and interface?

type Abstract_Fruit_Type is interface;
type Abstract_Fruit_Type is limited interface;
3 Answers

The key distinction is in ARM 3.9.4 12/2

A type derived from a nonlimited interface shall be nonlimited.

So if you want to implement the interface with a limited type then you must mark the interface with limited keyword. Then you will be able to implement this interface with limited or non-limited types.

If you omit limited keyword in interface description then all its implementations will be non-limited types only.

I think I figured it out: A limited interface can be used to extend a synchronized interface and its subcategories task interface and protected interface, while a (non-limited) interface cannot.

-- This is allowed

type Abstract_Type is limited interface;
type Task_Type is task interface and Abstract_Type;
-- This is not allowed
type Abstract_Type is interface;
type Task_Type is task interface and Abstract_Type;
-- error: (Ada 2005) task interface cannot inherit from non-limited interface

Both task interface and protected interface, and thus their parent -- all synchronized interface are inherently limited. Ada Reference Manual 2012 3.9.4 12/2 specifies the following

A type derived from a nonlimited interface shall be nonlimited.

Which means

A type that is limited cannot be derived from a nonlimited interface.

Event though interface types themselves are abstract, they are used to create concrete types, whose objects can be defined. Due to the above restriction, the limited keyword in limited interface is necessary.

Limited in Ada type declarations means "this thing does not have assignment."

In Ada's generic or in interface the keywords like limited or private are inverted: you are saying "the thing being represented here cannot be assumed to have property X". So you can instantiate a private-typed generic-parameter with an Integer, but inside the generic itself you can only count on the properties from the generic formal specification.

Related