对明显的矛盾寻求澄清关于弱类型语言 [英] Seeking clarification on apparent contradictions regarding weakly typed languages

查看:244
本文介绍了对明显的矛盾寻求澄清关于弱类型语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我明白强类型,但每次我找什么是弱类型我最后的例子高达发现,简单地强制/自动转换类型的编程语言的例子。

I think I understand strong typing, but every time I look for examples for what is weak typing I end up finding examples of programming languages that simply coerce/convert types automatically.

例如,这篇文章名为​​打字:强与弱,静态与动态说Python是强类型的,如果你尝试,因为你得到一个异常:

For instance, in this article named Typing: Strong vs. Weak, Static vs. Dynamic says that Python is strongly typed because you get an exception if you try to:

的Python

1 + "1"
Traceback (most recent call last):
File "", line 1, in ? 
TypeError: unsupported operand type(s) for +: 'int' and 'str'

然而,这样的事情有可能在Java和C#中,我们不认为他们弱类型只是为。

However, such thing is possible in Java and in C#, and we do not consider them weakly typed just for that.

的Java

  int a = 10;
  String b = "b";
  String result = a + b;
  System.out.println(result);

C#

int a = 10;
string b = "b";
string c = a + b;
Console.WriteLine(c);

在此的另一篇文章名为​​<一个href=\"http://www.i-programmer.info/programming/theory/1469-type-systems-demystified-part2-weak-vs-strong.html\">Weakly键入语言笔者说,Perl是弱类型只是因为我可以将字符串到数字,反之亦然,没有任何明确的转换。

In this another article named Weakly Type Languages the author says that Perl is weakly typed simply because I can concatenate a string to a number and viceversa without any explicit conversion.

的Perl

$a=10;
$b="a";
$c=$a.$b;
print $c; #10a

所以,同样的例子令Perl弱类型,而不是Java和C#?

So the same example makes Perl weakly typed, but not Java and C#?.

哎呀,这是混淆了

Gee, this is confusing

作者似乎暗示了$ P $语言pvents某些操作对不同类型的值的应用是强类型相反手段弱类型。

The authors seem to imply that a language that prevents the application of certain operations on values of different types is strongly typed and the contrary means weakly typed.

因此​​,在某些时候我已经感觉到提示认为,如果一种语言提供了大量的自动转换或类型之间的强制(像Perl)可能最终会被认为是弱类型,而只提供一些转换可能其他语言最终被认为是强类型。

Therefore, at some point I have felt prompted to believe that if a language provides a lot of automatic conversions or coercion between types (as perl) may end up being considered weakly typed, whereas other languages that provide only a few conversions may end up being considered strongly typed.

我倾向于认为,虽然,我一定是错在这intere pretation,我只是不知道为什么或如何解释它。

I am inclined to believe, though, that I must be wrong in this interepretation, I just do not know why or how to explain it.

所以,我的问题是:


  • 这是什么真正的意思的语言来真正的弱类型?

  • 您可以提到的是不相关的语言进行自动转换/自动强制弱打字的好例子?

  • 能否在语言弱类型和强同时键入?

推荐答案

更​​新:的这个问题是我的博客在2012年10月,第15届感谢伟大的问题!

UPDATE: This question was the subject of my blog on the 15th of October, 2012. Thanks for the great question!

这是什么真正的意思的语言来弱类型?

What does it really mean for a language to be "weakly typed"?

这意味着这种语言使用类型的系统,我觉得没意思。 A强类型的语言相反是一个类型的系统,我觉得愉快的语言。

It means "this language uses a type system that I find distasteful". A "strongly typed" language by contrast is a language with a type system that I find pleasant.

条款基本上是没有意义的,你应该避免。 维基百科列出的十一不同的含义的对强类型,其中有几个是矛盾的。这表明混乱正在创建的赔率是任何谈话涉及高的所谓强类型或弱类型。

The terms are essentially meaningless and you should avoid them. Wikipedia lists eleven different meanings for "strongly typed", several of which are contradictory. This indicates that the odds of confusion being created are high in any conversation involving the term "strongly typed" or "weakly typed".

所有你真的可以说有把握的是,在讨论强类型的语言在类型系统一些额外的限制,无论是在运行时或编译的时候,也正在讨论一种弱类型的语言缺乏。什么限制可能是不能没有进一步的上下文中确定。

All that you can really say with any certainty is that a "strongly typed" language under discussion has some additional restriction in the type system, either at runtime or compile time, that a "weakly typed" language under discussion lacks. What that restriction might be cannot be determined without further context.

而不是使用强类型和弱类型,你应该详细描述你的意思是什么样的类型安全的。例如,C#是一个的静态类型的语言和的类型安全的语言和的内存安全的语言的在大多数情况下。 C#允许所有三种这些形式的强打字受到侵犯。演员经营者违反静态类型;它编译我知道更多关于运行时类型这个前pression比你做的说。如果开发商是错误的,那么运行时会以保护式安全抛出异常。如果开发人员希望到破裂类型安全或存储器的安全性,它们可以通过使不安全块关闭式安全系统这样做。在一个不安全块,你可以使用指针魔法治疗一个int作为一个float(违反类型安全)或写信给你没有自己的记忆。 (违反内存安全。)

Instead of using "strongly typed" and "weakly typed", you should describe in detail what kind of type safety you mean. For example, C# is a statically typed language and a type safe language and a memory safe language, for the most part. C# allows all three of those forms of "strong" typing to be violated. The cast operator violates static typing; it says to the compiler "I know more about the runtime type of this expression than you do". If the developer is wrong, then the runtime will throw an exception in order to protect type safety. If the developer wishes to break type safety or memory safety, they can do so by turning off the type safety system by making an "unsafe" block. In an unsafe block you can use pointer magic to treat an int as a float (violating type safety) or to write to memory you do not own. (Violating memory safety.)

C#强加了是在编译时和在运行时检查类型的限制,从而使其成为强类型的语言相比,少做编译时检查以下运行时检查的语言。 C#也可以让你在特殊情况下做一个最终奔波这些限制,使之成为一个弱类型语言与语言不容许你做这样一个最终的运行进行比较。

C# imposes type restrictions that are checked at both compile-time and at runtime, thereby making it a "strongly typed" language compared to languages that do less compile-time checking or less runtime checking. C# also allows you to in special circumstances do an end-run around those restrictions, making it a "weakly typed" language compared with languages which do not allow you to do such an end-run.

这是不是真的?这是不可能的说;它取决于来看扬声器和他们对各种语言特性的态度的地步。

Which is it really? It is impossible to say; it depends on the point of view of the speaker and their attitude towards the various language features.

这篇关于对明显的矛盾寻求澄清关于弱类型语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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