Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two types are equivalent if an operand of one type in an expression is substitut

ID: 3822622 • Letter: T

Question

Two types are equivalent if an operand of one type in an expression is substituted for one of the other type, without coercion. There are two approaches to defining type equivalence. Name type equivalence means that two variables have equivalent types if they are defined either in the same declaration or in declarations that use the same type name. Structure type equivalence means that two variables have equivalent types if their types have identical structures. (a) The Pascal language adopts name type equivalence. Consider the following declarations: a1: array [1..10] of integer; a2: array [1..10] of integer; According to name type equivalence, the variables a1 and a2 are considered to have distinct and non-equivalent types. In other words, values of a1 cannot be assigned to a2, and vice versa. Suggest two ways of defining a1 and a2 so that they have the same type. (b) On the other hand, structure type equivalence is more lenient. Consider the following declarations: a1: array [1.. 10] of integer; a2: array [1 .. 20] of integer; The types of the variables a1 and a2 are considered to be equivalent, even though they have different names and different subscript ranges. It is because both types have lo elements of the integer type. Give two scenarios in which structure type equivalence is undesirable.

Explanation / Answer

b.) Scenario 1:-

Structure type Equivalence match up classes to interfaces which may not really implement the correct contract. For example, if you have this interface:

then the following class will implicitly be considered to implement IBananaProvider in a structural type system. However, the class violates the post condition that the returned banana is never null:

Scenario 2:-

In following scenario with structural equivalence, you would end up with a miscalculation that will lead to unpredictable behaviour, depending on the type of application and system this could lead to serious financial loss or even death. Such a logical bug is also very difficult to track down without strict name equivalence in place.