In Ada, I have commonly seen something like this:
type Number is new Integer;
What is the point of this? Can't you just be happy with an Integer
?
I have also seen code such as:
type Small_Number is range 1..5;
This makes sense to me; I can see why this would be useful. But why, in any case would you choose to use the former example?
I agree that
(which is not a "renaming" of a type) looks like bad style, but there may be a perfectly good reason for it. For example:
Number
to be a distinct type, but with the same range asInteger
.Number
to match the range of an array type with anInteger
derived index.More commonly I have seen code like this:
This means that you are not going to assign your Pounds to Euros to Dollars by accident.
If you want to convert between the two you would need to either do an explicit cast, or write a conversion routine, both of which would take the applicable exchange rate into account.
(Now I think about it further, Float would have been better than Integer for this example!)
The point is that
Number
is a new type, quite distinct fromInteger
.That means more control over parameters and such since you cannot use an
Integer
where aNumber
is required; this aids with encapsulation.It's quite plausible that you want to keep this level of control and perhaps plan for the future where you may end up with
Number
being totally different fromInteger
.