应用语言支持匈牙利语吗? [英] Language with apps hungarian support?
问题描述
基本上,我想知道是否存在一种语言,该代码将是无效的,因为即使counter
和distance
都是int
,它们在现实世界中仍表示不兼容的类型 :
Basically, I wonder if a language exists where this code will be invalid because even though counter
and distance
are both int
under the hood, they represent incompatible types in the real world:
#include <stdio.h>
typedef int counter;
typedef int distance;
int main() {
counter pies = 1;
distance lengthOfBiscuit = 4;
printf("total pies: %d\n", pies + lengthOfBiscuit);
return 0;
}
使用"gcc -pedantic -Wall"以及我尝试过的所有其他语言进行编译,而不会发出警告.禁止不小心添加计数器和距离似乎是个好主意,那么语言支持在哪里?
That compiles with no warnings with "gcc -pedantic -Wall" and all other languages where I've tried it. It seems like it would be a good idea to disallow accidentally adding a counter and a distance, so where is the language support?
(顺便提一句,提示此问题的真实示例是PHP和Python中的Web开发工作-我试图制作"HTML转义字符串","SQL转义字符串"和原始危险的用户输入"不兼容的,但我似乎能得到的最好的建议就是匈牙利文的应用程序,这里建议-> http://www.joelonsoftware.com/articles/Wrong.html <-仍然依靠人工检查(错误的代码看起来错误")而不是编译器支持(错误的代码是错误"))
(Incidentally, the real-life example that prompted this quesion was web dev work in PHP and Python -- I was trying to make "HTML-escaped string", "SQL-escaped string" and "raw dangerous user input" incompatible, but the best I can seem to get is apps hungarian notation as suggested here --> http://www.joelonsoftware.com/articles/Wrong.html <-- and that still relies on human checking ("wrong code looks wrong") rather than compiler support ("wrong code is wrong"))
推荐答案
在Ada中,您可以使用相同的表示形式,但仍然是不同的类型.在C或C ++中,强typedef"将是什么(如果存在).
In Ada you can have types that use the same representation, but are still distinct types. What a "strong typedef" would be (if it existed) in C or C++.
您可以的话
type counter is new Integer;
type distance is new Integer;
创建两个行为类似于整数但不能混合的新类型.
to create two new types that behave like integers, but cannot be mixed.
这篇关于应用语言支持匈牙利语吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!