弱类型不再需要了吗?

发布于 2024-09-11 06:39:20 字数 218 浏览 13 评论 0原文

我来自静态/强类型语言背景(java),最近开始学习Python,我想我看到了动态/强类型语言的价值。

现在我想知道弱类型是否是可取的。

通过 stackoverflow,我只找到了说明它具有性能优势的解释。由于这种性能优势的重要性在当今的编程世界中正在下降,弱类型实际上已经死亡了吗?例如,未来有哪个优秀的语言设计师会考虑让他/她的语言成为弱类型吗?

如果是这样,为什么?

I come from a statically/strongly typed language background (java), and I recently started to learn python and I think I see the value of dynamic/strongly typed language.

Now I'm wondering whether weak typing can be ever desirable.

Going through stackoverflow, I only found explanations that say it has performance benefits. Since the weight of such performance benefit are declining in today's programming world, is weak typing practically dead? For example, would any good language designer in the future consider making his/her language weak-typed?

If so, why?

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

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

发布评论

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

评论(5

御守 2024-09-18 06:39:20

弱类型主要用于低级编程。例如,从磁盘读取整数或字符串的函数必须采用字节序列并得出整数或字符串。对于强类型来说,这要困难得多。

Weak typing is primarily useful in low-level programming. A function to read an integer or string off a disk, for example, will have to take a sequence of bytes and come up with an integer or string. That's much harder to do with strong typing.

霊感 2024-09-18 06:39:20

弱类型曾经是、也永远不会是必要的。它不会以任何方式影响语义。相当于自动调用转换函数。一个例子是在 C 中比较 int 和 float:1 == 1.0。这将毫无怨言地编译。除了“方便”之外,没有其他原因,例如,如果 int_to_float 有一个函数并执行 intToFloat(1) == 1.0,那么它显然会内置到 C 中,并且此代码会执行此操作与将 int 转换为 float 完全相同。弱类型是安全性和便利性之间的权衡。

虽然我真的不明白必须记住 (operator × operand × operand) (float / int), (float == int), (string + int), (int + string), (字符串 + 布尔值)、(整数 + 长整型)等

Weak typing was and never will be necessary. It does not effect semantics in any way what so ever. It is the equivalent of calling conversion functions automatically. An example is comparing an int to float in C: 1 == 1.0. This will compile without any complaints. There is no reason for this other than "convenience", for example if there was a function from int_to_float and doing intToFloat(1) == 1.0, it would obviously be built into C and this code would do the exact same thing as when an int is converted to a float. Weak typing is a tradeoff between safety and convenience.

Though I really don't understand what's convenient about having to memorize every single case of (operator × operand × operand) (float / int), (float == int), (string + int), (int + string), (string + boolean), (int + long), etc.

り繁华旳梦境 2024-09-18 06:39:20

这不是火焰,只是我的一般经历。在 10 多年编写和维护代码的过程中,我只遇到过与类型相关的错误不到六次。然而,在编写移动字节的低级代码时,我开始强烈讨厌类型的想法。长期以来,当编程文化采用良好可读性的理想时,我一直认为打字的想法就不再必要了。

当然,根据语言和编码风格,键入可以帮助或阻碍代码理解,所以我不太介意键入语言。

因此,只要有人像我一样看不到类型的任何优势,就总会有设计为无类型或弱类型的语言。

考虑到错误的跟踪记录与所使用的语言(根据我的经验和我在开源代码中看到的)强类型或弱类型不会对代码质量产生太大影响。考虑到我根据我的经验实际阅读过的代码,我也会说它不会对代码的可读性产生太大影响。对我来说,这只是一个品味问题(虽然我知道强势型的人会不同意,但他们只是无法用事实来支持他们的不同意见,这只是他们的直觉告诉他们不同意)。好吧,最后一句话几乎要火了。我最好现在就停下来。

This is not a flame but just my experience in general. In more than 10 years writing and maintaining code I've only come across type related bugs less than half a dozen times. I have however come to strongly hate the idea of types when writing low level code which moves bytes around. I have long considered the idea of typing to be not necessary anymore when the culture of programming adopted the ideals of good readability.

Of course depending on the language and coding style typing can either help or impede code understanding so I don't mind typed languages too much.

So, as long as there are people out there who, like me, don't see any advantages in types there will always be languages designed to be typeless or weakly typed.

Considering the track record of bugs vs what language used (both in my experience and from what I see on open source code) strong or weak typing does not affect code quality much. Considering the code I've actually read in my experience I would also say it does not affect code readability much. To me it's just a matter of taste (though I know strong type people disagree, they just can't back up their disagreement with facts, it's just their gut telling them to disagree). OK, that last sentence was almost flaming. I'd better stop now.

迷雾森÷林ヴ 2024-09-18 06:39:20

如果您不必担心类型,似乎您可以让某些东西更快地工作。有些语言并不强迫您担心它们。

这一切都取决于您想要实现的目标。

Seems like you could get something working faster if you didn't have to worry about types. Some languages don't force you to worry about them.

It all lies in what you're trying to accomplish.

烈酒灼喉 2024-09-18 06:39:20

它减少了大量的编码时间,同时又不会增加太多调试时间;在测试中发现类型错误的速度几乎与编译器发现它们的速度一样快,但您不必输入 int fooBar = 2 它也有助于不要拥有 4+ Number 类型,但是......

It trims down a lot of coding time, while not increasing debugging time all that much; Type errors are found in testing almost as quick as a compiler finds them, but you didn't have to type int fooBar = 2 It also helps not to have 4+ Number types, however...

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