问题描述
如何在Ada中找到变量的数据类型?
How can I find the datatype of a variable in Ada?
例如,给定
INT : integer;
如何为该变量打印数据类型为整数?
how can I print "the datatype is integer" for this variable?
在Python中,可以使用 type()
查找类型。 Ada中是否有任何类似的函数来查找变量的数据类型?
In Python, type()
can be used to find the type. Is there any similar function in Ada to find the datatype of a variable?
推荐答案
Ada是一种强类型语言,当您声明一个变量,然后指定其类型。因此,没有函数可以返回变量的类型,就像语言中没有类型的变量一样。程序已经知道类型。
Ada is a strongly typed language, and when you declare a variable you specify its type. So there is no use for a function to return the variable's type, as there would be in languages with untyped variables. The program already knows the type.
如果声明的变量 X
的类型为 T 'Class
,则实际值的类型可以是 T
或从 T $ c $派生的任何类型c>。在这种情况下,您可以使用
X'Tag
来获取值实际类型的 tag ,这是您可以获取的最接近的类型实际类型。有了标签后,您可以执行以下操作:获取类型名称( Ada.Tags
中有此功能),将其与某种类型的标签进行比较以查看如果是那种类型,等等。但是 Integer
不是标记类型,因此不能在'Tag
上使用它就没有用了。
If a variable X
is declared with type T'Class
, then the type of the actual value can be T
or any type derived from T
. In that case, you can use X'Tag
to get the tag of the value's actual type, which is the closest you can come to getting the actual type. Once you have a tag, you can do things like getting the type's name (there are functions for this in Ada.Tags
), comparing it to the tag of some type to see if it's that type, etc. But Integer
is not a tagged type, so you can't use 'Tag
on it and there would be no use for it.
这篇关于ADA中变量的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!