为什么 unsigned int 不符合 CLS?

发布于 2024-07-04 03:09:41 字数 58 浏览 14 评论 0原文

为什么无符号整数不符合 CLS?

我开始认为类型规范只是为了性能而不是为了正确性。

Why are unsigned integers not CLS compliant?

I am starting to think the type specification is just for performance and not for correctness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷情 2024-07-11 03:09:42

无符号整数不符合 CLS,因为它们在某些语言之间不可互操作。

Unsigned integers are not CLS compliant because they're not interoperable between certain languages.

顾挽 2024-07-11 03:09:42

无符号整数在现实生活中不会给你带来那么多好处,但是拥有超过一种类型的整数会给你带来痛苦,所以很多语言只有有符号整数。

符合 CLS 的目的是允许在多种语言中使用一个类...

请记住,没有人让您符合 CLS。

您仍然可以内使用无符号整数 。方法,或作为私有方法的参数,因为它只是符合 CLS 限制的公共 API。

Unsigned int's don't gain you that much in real life, however having more than 1 type of int gives you pain, so a lot of languages only have singed ints.

CLS compliant is aimed at allowing a class to be made use of from lots of languages…

Remember that no one makes you be CLS compliant.

You can still use unsigned ints within a method, or as parms to a private method, as it is only the public API that CLS compliant restricts.

濫情▎り 2024-07-11 03:09:42

我怀疑,部分问题在于 C 中的无符号整数类型需要充当抽象代数环的成员而不是数字 [这意味着,例如,如果无符号 16 位整数变量等于 0 ,递减它需要来产生 65,535,如果它等于 65,535,则需要递增它来产生零。] 有时这种行为非常有用,但是数字类型表现出这种行为可能会违背了某些语言的精神。 我推测省略无符号类型的决定可能早于支持已检查和未检查数字上下文的决定。 就我个人而言,我希望无符号数和代数环有单独的整数类型; 将一元减运算符应用于无符号 32 位数字应产生 64 位有符号结果 [对除零以外的任何值取反将产生负数],但将一元减运算符应用于环类型应产生该环内的加法逆元。

无论如何,无符号整数不符合 CLS 的原因是 Microsoft 决定语言不必支持无符号整数才能被视为“CLS 兼容”。

Part of the issue, I suspect, revolves around the fact that unsigned integer types in C are required to behave as members of an abstract algebraic ring rather than as numbers [meaning, for example, that if an unsigned 16-bit integer variable equals zero, decrementing it is required to yield 65,535, and if it's equal to 65,535 then incrementing it is required to yield zero.] There are times when such behavior is extremely useful, but numeric types exhibit such behavior may have gone against the spirit of some languages. I would conjecture that the decision to omit unsigned types probably predates the decision to support both checked and unchecked numeric contexts. Personally, I wish there had been separate integer types for unsigned numbers and algebraic rings; applying a unary minus operator to unsigned 32-bit number should yield a 64-bit signed result [negating anything other than zero would yield a negative number] but applying a unary minus to a ring type should yield the additive inverse within that ring.

In any case, the reason unsigned integers are not CLS compliant is that Microsoft decided that languages didn't have to support unsigned integers in order to be considered "CLS compatible".

一萌ing 2024-07-11 03:09:42

并非所有语言都有无符号整数的概念。 例如,VB 6 没有无符号整数的概念,我怀疑这促使 VB7/7.1 的设计者决定不实现(现在已在 VB8 中实现)。

去引用:

http://msdn.microsoft.com/en-us/library/ 12a7a7h3.aspx

CLS 的设计足够大以包含该语言
开发人员通常需要的结构,但足够小
大多数语言都能够支持它。 此外,任何语言
构造使得无法快速验证类型安全性
的代码被排除在 CLS 之外,因此所有符合 CLS 的语言
如果他们选择这样做,可以生成可验证的代码。

更新:几年前我确实想知道这个问题,虽然我不明白为什么 UInt 不能进行类型安全验证,但我猜 CLS 人员必须在某个地方设置一个截止点来确定最低基线是多少支持的值类型数量。 另外,当您考虑从长远来看,越来越多的语言被移植到 CLR 时,如果绝对没有概念,为什么还要强迫它们实现无符号整数以获得 CLS 合规性呢?

Not all languages have the concept of unsigned ints. For example VB 6 had no concept of unsigned ints which I suspect drove the decision of the designers of VB7/7.1 not to implement as well (it's implemented now in VB8).

To quote:

http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

The CLS was designed to be large enough to include the language
constructs that are commonly needed by developers, yet small enough
that most languages are able to support it. In addition, any language
construct that makes it impossible to rapidly verify the type safety
of code was excluded from the CLS so that all CLS-compliant languages
can produce verifiable code if they choose to do so.

Update: I did wonder about this some years back, and whilst I can't see why a UInt wouldn't be type safety verifiable, I guess the CLS guys had to have a cut off point somewhere as to what would be the baseline minimum number of value types supported. Also when you think about the longer term where more and more languages are being ported to the CLR why force them to implement unsigned ints to gain CLS compliance if there is absolutely no concept, ever?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文