Substitutable type samples

Sample files

Please open ModelManyViews.lava in LavaPE.

Please open CarMeeting.lava in LavaPE.

Topic

Classes and packages with type parameters ("virtual types"), substitutable types.

Overview

If the type T of any kind of Lava variable is marked as being substitutable (by putting the name of the type in braces: {T}) then the variable may assume values (= concrete Lava objects) whose types need not be derived in the traditional sense from T, but they may also be derived from T in the sense of pattern derivation.

ModelManyViews:

While the ModelView sample supports only a single type of views, the ModelManyViews sample allows you to derive many different types of views from "View" and to append corresponding view objects to the heterogeneous list vList of type "ViewList" whose elements have the substitutable virtual type setElem={~View}. (The tilde designates variable state objects.)

CarMeeting:

The CarMeeting sample corresponds also to a similar sample in [16]. It uses a substitutable virtual type to allow subtype-substitution for a formal parameter of a function. The type of the formal parameter is a substitutable but homogeneous list type (whose element type is not substitutable in turn!): You cannot add elements of a type that is p-derived from Vehicle (for instance a Car) to a VehicleList unless this happens in the proper pattern context (for instance that of CarList).

Where to look and what to do

ModelManyViews:

Look at the declaration of member variable vList of class Model. It is of type ViewList, whose declaration is nested into that of Model. ViewList defines a heterogeneous list whose elements may be of any type that is c- or p-derived from View.

Look at the declaration of member function registerView of class Model. Its only input parameter is of (substitutable) type {~View} and thus accepts all kinds of views whose types are c- or p-derived from View. Look into the implementation of registerView and see how the respective view is added to Model's ViewList vList.

CarMeeting:

Look at the declaration of member function meet of class Meeting. The type of its first input parameter is substitutable. Look into the exec of initiator meetingDemo and read the comments. Look into the implementation of function meet and read the comments.