In this chapter we describe the parameterization of types by what are known as discriminants. Discriminants are components which have special properties. All composite types other than arrays can have discriminants. In this chapter we deal with the properties of discriminants in general and their use with record types (both tagged and untagged); their use with task and protected types is discussed in Chapter 20.
Discriminants can be of a discrete type or an access type. In the latter case the access type can be a named access type or it can be anonymous. A discriminant of an anonymous access type is called an access discriminant by analogy with an access parameter.
We start by dealing with discrete discriminants of untagged types.
Discriminated record types
In the record types we have seen so far there was no formal language dependency between the components. Any dependency was purely in the mind of the programmer as for example in the case of the private type Stack in Section 12.4 where the interpretation of the array S depended on the value of the integer Top.
In the case of a discriminated record type, some of the components are known as discriminants and the remaining components can depend upon these. The discriminants can be thought of as parameterizing the type and the syntax reveals this analogy.
As a simple example, suppose we wish to write a package providing various operations on square matrices and that in particular we wish to write a function Trace which sums the diagonal elements of a square matrix. We could contemplate using the type Matrix of Section 8.2
type Matrix is array (Integer range < >, Integer range < >) of Float;
but the function would then have to check that the matrix passed as an actual parameter was indeed square. We would have to write something like
function Trace(M: Matrix) return Float is
Sum: Float := 0.0;
begin
if M'First(1) /= M'First(2) or M'Last(1) /= M'Last(2) then raise Non_Square;
end if;
for I in M'Range loop
Sum := Sum + M(I, I);
end loop;
return Sum;
end Trace;
This is somewhat unsatisfactory; we would prefer to use a formulation which ensured that the matrix was always square and had a lower bound of 1. We can do this using a discriminated type. Consider
type Square(Order: Positive) is
record
Mat: Matrix(1 .. Order, 1 .. Order);
end record;
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.