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

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

问题描述

我想我了解感谢您提出的好问题!

<小时><块引用>

弱类型"语言的真正含义是什么?

这意味着这种语言使用了一种我觉得令人反感的类型系统".相比之下,强类型"语言是一种我觉得令人愉快的类型系统的语言.

这些术语基本上毫无意义,您应该避免使用它们.维基百科列出了强类型"的十一种不同含义,其中有几个是矛盾的.这表明在任何涉及强类型"或弱类型"的对话中产生混淆的几率都很高.

您真正可以肯定地说,正在讨论的强类型"语言在类型系统中具有一些额外的限制,无论是在运行时还是编译时,正在讨论的弱类型"语言都没有.如果没有进一步的背景,就无法确定该限制是什么.

不要使用强类型"和弱类型",您应该详细描述您指的是哪种类型安全.例如,C# 是一种静态类型语言和一种类型安全语言和一种内存安全语言,在大多数情况下em>.C# 允许违反所有这三种形式的强"类型.cast 运算符违反了静态类型;它对编译器说我比你更了解这个表达式的运行时类型".如果开发者出错,那么运行时会抛出异常以保护类型安全.如果开发人员希望破坏类型安全或内存安全,他们可以通过创建不安全"块来关闭类型安全系统.在不安全的块中,您可以使用指针魔法将 int 视为浮点数(违反类型安全)或写入不属于您的内存.(违反内存安全.)

C# 强加了在编译时和运行时都进行检查的类型限制,从而使其成为一种强类型"语言,与那些编译时检查较少或运行时检查较少的语言相比.C# 还允许您在特殊情况下围绕这些限制进行最终运行,与不允许您执行此类最终运行的语言相比,C# 使其成为弱类型"语言.

这是真的吗?不能说;这取决于说话者的观点和他们对各种语言特征的态度.

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.

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'

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);

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

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

Gee, this is confusing

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.

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.

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.

So, my questions are:

  • What does it really mean for a language to be truly weakly typed?
  • Could you mention any good examples of weakly typing that are not related to automatic conversion/automatic coercion done by the language?
  • Can a language be weakly typed and strongly typed at the same time?

解决方案

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"?

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.

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# 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天全站免登陆