Restricted Types
Structure and resource types can be restricted. Restrictions are interfaces. Restricted types only allow access to a subset of the members and functions of the type that is restricted, indicated by the restrictions.
The syntax of a restricted type is T{U1, U2, ... Un}
,
where T
is the restricted type, a concrete resource or structure type,
and the types U1
to Un
are the restrictions, interfaces that T
conforms to.
Only the members and functions of the union of the set of restrictions are available.
Restricted types are useful for increasing the safety in functions that are supposed to only work on a subset of the type. For example, by using a restricted type for a parameter's type, the function may only access the functionality of the restriction: If the function accidentally attempts to access other functionality, this is prevented by the static checker.
In addition to restricting concrete types is also possible
to restrict the built-in types AnyStruct
, the supertype of all structures,
and AnyResource
, the supertype of all resources.
For example, restricted type AnyResource{HasCount}
is any resource type
for which only the functionality of the HasCount
resource interface can be used.
The restricted types AnyStruct
and AnyResource
can be omitted.
For example, the type {HasCount}
is any resource that implements
the resource interface HasCount
.
Only concrete types may be restricted, e.g., the restricted type may not be an array,
the type [T]{U}
is invalid.
Restricted types are also useful when giving access to resources and structures to potentially untrusted third-party programs through references, which are discussed in the next section.