Sample code detailing the difference in typing between Ada and Java

I am trying to come up with a good example that details the difference between strong Ada typing and strong Java typing. Does anyone have a good example to identify these differences in type conversion using integer based values?

+2
source share
1 answer

This may be useful from an Ada point of view.

There are two main forms of type declarations: a new type and a subtype.

A new type is considered as a completely different type from any previous type, even if it has the same range of values. Objects of a new type can only be assigned to variables of a new type, etc.

A subtype is derived from an existing type, shares a subset of its values, and is considered to be essentially the same type as for assignment.

The art of using a type system requires little practice, and this is the choice between types and subtypes - this is one area where it matters. This makes the difference between an unsuccessful struggle with the type system and the presence of programs that are easily combined with each other.

  • Declare a new type, where its values ​​mean something new, something you do not need or want to mix with other quantities.
  • Declare a subtype where its values ​​are related to existing things.

Example: imagine building automation, including its lift system:

type Floor is new Integer range -3 ..135; 

There are several underground garages, a basement, 0 - the first floor (for a European, not an American building!) And 135 floors above it, which makes it one floor higher than the tower of hell.

This can be a safe new type, because little can interfere with the Floor variable with anything else, and very little need to mathematically combine the floor with any other amount. Elevators must go there, and what about it. In this case, having sex as a new type may catch some mistakes, but the added safety is without the prospect of pain.

Using types or subtypes as array indices and contour boundaries is a good way to make access to access restrictions unlikely.

 Array(Floor) of ... for f in Floor loop ... 

If you need to assign an integer variable to gender, type conversion signals both the compiler and anyone reading the code that this is intentional.

 subtype Population is Natural range 0 .. 10000; 

We need to know how many people occupy the building to ensure safety, fire safety, heating or cooling, and other purposes. Its range has severe restrictions at both ends: for this building, let's say that more than 10,000 people were forbidden by fire safety rules.

The basic type is Natural (the Integer subtype itself), not Integer, because the population can never be less than zero.

But creating a new type of Population can cause endless type conversion problems. The creation of this subtype facilitates the use of the Population in calculating the heat load, elevator planning strategies, and water use forecasts for several examples.

The fact that it is a subtype, not just an Integer, still provides useful forms of protection; while integers can be assigned to Population variables and mixed with them in expressions, any attempt to assign values ​​outside the range will be flagged as errors. If they cannot be detected at compile time, they will throw an exception at runtime. And so there will be 10001 people trying to enter the building.

I will let Java experts explain the best way to achieve these strategies in Java.

+7
source

Source: https://habr.com/ru/post/1273599/


All Articles