重命名Ada中的类型有什么意义 [英] What is the point of renaming Types in Ada

查看:70
本文介绍了重命名Ada中的类型有什么意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ada中,我经常看到这样的情况:

In Ada, I have commonly seen something like this:

type Number is new Integer;

这是什么意思? 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?

推荐答案

更常见的是,我看到这样的代码: / p>

More commonly I have seen code like this:

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 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.

(现在我再想一想,在这个例子中,Float会比Integer更好!)

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

这篇关于重命名Ada中的类型有什么意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆