next up previous contents
Next: Type Conversions Up: The MPD Programming Language Previous: Assignment Operators   Contents

Subsections

Type Equivalence

Type equivalence in MPD is based on structural equivalence. An object can be assigned to another object if they have the same structure.

arrays
two arrays are structurally equivalent if they have the same dimension and their elements are structurally equivalent.

records
two records are structurally equivalent if they have the same numbers of fields and the corresponding fields have equivalent types, regardless of the names of the fields.

enumerations
two enumerations are structurally equivalent if they define the same number of literals.

Consider the following:

type r1 = rec(int a; real b)
type r2 = r1
type r3 = rec(int c; real d)
type r4 = rec(real c; int d)

type prim_color   = enum(red, blue, yellow)
type machine_name = enum(ivy, holly, thyme)
type books        = enum(dictionary, thesaurus)

Obviously r1 and r2 are equivalent, but so are also r1 and r3 and thus r2 and r3. Because of the reversed order of the fields of r4 it is not equivalent to any of the other types. The enumeration prim_color is equivalent to machine_name but neither is equivalent to books.


 

Warning!

You should avoid using structural equivalence to assign between intentionally unequal types.


next up previous contents
Next: Type Conversions Up: The MPD Programming Language Previous: Assignment Operators   Contents
David Sands 2003-09-05