What's the point of renaming types in Ada

In Ada, I usually saw something like this:

type Number is new Integer; 

What is the meaning of this? Can't you be happy with Integer ? I also saw the code, for example:

 type Small_Number is range 1..5; 

It makes sense to me; I understand why this would be helpful. But why, in any case, would you prefer to use the previous example?

+1
source share
3 answers

Most often I saw this code:

 Type Pounds is new Integer; Type Euros is new Integer; Type Dollars is new Integer; 

This means that you are not going to assign your pounds to euros in dollars by accident.

If you want to convert between the two, you need to either perform an explicit conversion or write a conversion procedure that takes into account the applicable exchange rate.

(Now I think about it further, Float would be better than Integer for this example!)

+10
source

The fact is that Number is a new type, other than Integer .

This means more control over parameters, etc., since you cannot use Integer where Number is required; it helps with encapsulation.

It is very plausible that you want to maintain this level of control and perhaps plan for the future, where you may end up with Number completely different from Integer .

+5
source

I agree that

 type Number is new Integer; 

(which is not a "renaming" type) looks like a bad style, but there could be a good reason for this. For instance:

  • You want Number be a separate type, but with the same range as Integer .
  • You want Number match an array type range with an Integer index.
+1
source

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


All Articles