有人能告诉我强类型和弱类型是什么意思以及哪个更好吗?

发布于 2024-07-09 15:38:23 字数 33 浏览 16 评论 0原文

有人能告诉我强类型和弱类型是什么意思以及哪个更好吗?

Can someone tell me what Strong typing and weak typing means and which one is better?

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

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

发布评论

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

评论(8

長街聽風 2024-07-16 15:38:23

这将是理论答案,但实践方面似乎被忽视了......

强类型意味着你不能在需要另一种类型的变量时使用一种类型的变量(或者这样做受到限制)。 弱类型意味着您可以混合不同的类型。 例如,在 PHP 中,您可以混合使用数字和字符串,并且 PHP 不会抱怨,因为它是弱类型语言。

$message = "You are visitor number ".$count;

如果它是强类型的,则必须将 $count 从整数转换为字符串,通常使用强制转换:

$message = "you are visitor number ".(string)$count;

...或函数:

$message = "you are visitor number ".strval($count);

至于哪个更好,这是主观的。 强类型的拥护者会告诉您,它将帮助您避免一些错误和/或错误,并帮助传达变量的用途等。他们还会告诉您,弱类型的拥护者将强类型称为“< em>不必要的语言废话,被常识认为毫无意义”,或类似的东西。 作为弱类型群体的持牌成员,我不得不说他们有我的号码……但我也有他们的,并且可以将其放入字符串中:)

That'll be the theory answers taken care of, but the practice side seems to have been neglected...

Strong-typing means that you can't use one type of variable where another is expected (or have restrictions to doing so). Weak-typing means you can mix different types. In PHP for example, you can mix numbers and strings and PHP won't complain because it is a weakly-typed language.

$message = "You are visitor number ".$count;

If it was strongly typed, you'd have to convert $count from an integer to a string, usually with either with casting:

$message = "you are visitor number ".(string)$count;

...or a function:

$message = "you are visitor number ".strval($count);

As for which is better, that's subjective. Advocates of strong-typing will tell you that it will help you to avoid some bugs and/or errors and help communicate the purpose of a variable etc. They'll also tell you that advocates of weak-typing will call strong-typing "unnecessary language fluff that is rendered pointless by common sense", or something similar. As a card-carrying member of the weak-typing group, I'd have to say that they've got my number... but I have theirs too, and I can put it in a string :)

ぺ禁宫浮华殁 2024-07-16 15:38:23

“强类型”及其相反的“弱类型”的含义相当弱,部分原因是“强类型”的概念可能会根据您询问的对象而有所不同。 例如,C 被不同的作者称为“强类型”和“弱类型”,这实际上取决于你将它与什么进行比较。

一般来说,如果一个类型系统能够表达与另一个或更多类型系统相同的约束,那么它应该被认为是更强的。 然而,两种类型的系统常常不具有可比性——一种系统可能具有另一种系统所缺乏的功能,反之亦然。 对相对优势的任何讨论都取决于个人喜好。

拥有更强大的类型系统意味着编译器或运行时将报告更多错误,这通常是一件好事,尽管它可能以必须手动提供更多类型信息为代价,这可能被认为是不值得的努力。 我认为“强类型”通常更好,但你必须考虑成本。

同样重要的是要认识到“强类型”经常被错误地使用,而不是“静态类型”甚至“清单类型”。 “静态类型”意味着在编译时进行类型检查,“清单类型”意味着显式声明类型。 清单类型可能是使类型系统变得更强大的最著名的方法(想想 Java),但是您可以通过其他方式(例如类型推断)来增强强度。

"Strong typing" and its opposite "weak typing" are rather weak in meaning, partly since the notion of what is considered to be "strong" can vary depending on whom you ask. E.g. C has been been called both "strongly typed" and "weakly typed" by different authors, it really depends on what you compare it to.

Generally a type system should be considered stronger if it can express the same constraints as another and more. Quite often two type systems are not be comparable, though -- one might have features the other lacks and vice versa. Any discussion of relative strengths is then up to personal taste.

Having a stronger type system means that either the compiler or the runtime will report more errors, which is usually a good thing, although it might come at the cost of having to provide more type information manually, which might be considered effort not worthwhile. I would claim "strong typing" is generally better, but you have to look at the cost.

It's also important to realize that "strongly typed" is often incorrectly used instead of "statically typed" or even "manifest typed". "Statically typed" means that there are type checks at compile-time, "manifest typed" means that the types are declared explicitly. Manifest-typing is probably the best known way of making a type system stronger (think Java), but you can add strength by other means such as type-inference.

天暗了我发光 2024-07-16 15:38:23

我想重申,弱类型与动态类型不同。

这是一篇关于该主题的文章,写得相当好,如果您不确定强类型系统、弱类型系统、静态类型系统和动态类型系统之间的差异,我绝对建议您阅读它。 它详细描述了这些差异,比简短答案中预期的要好得多,并且有一些非常有启发性的示例。

http://en.wikipedia.org/wiki/Type_system

I would like to reiterate that weak typing is not the same as dynamic typing.

This is a rather well written article on the subject and I would definitely recommend giving it a read if you are unsure about the differences between strong, weak, static and dynamic type systems. It details the differences much better than can be expected in a short answer, and has some very enlightening examples.

http://en.wikipedia.org/wiki/Type_system

追我者格杀勿论 2024-07-16 15:38:23

强类型是现代编程语言中最常见的类型模型。 这些语言有一个简单的功能 - 了解运行时的类型值。 我们可以说强类型语言可以防止两种或多种不同类型之间的混合操作。 下面是 Java 中的一个示例:

String foo = "Hello, world!";
Object obj = foo;

String bar = (String) obj;
Date baz = (Date) obj; // This line will throw an error

前面的示例将完美运行,直到程序到达将抛出 ClassCastException 的最后一行代码,因为 Java 是强类型编程语言。

当我们谈论弱类型语言时,Perl 就是其中之一。 下面的示例展示了 Perl 如何在混合两种不同类型时不会出现任何问题。

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

我希望你觉得这很有用,

谢谢。

Strong typing is the most common type model in modern programming languages. Those languages have one simple feature - knowing about type values in run time. We can say that strong typed languages prevent mixing operations between two or more different kind of types. Here is an example in Java:

String foo = "Hello, world!";
Object obj = foo;

String bar = (String) obj;
Date baz = (Date) obj; // This line will throw an error

The previous example will work perfectly well until program hit the last line of code where the ClassCastException is going to be thrown because Java is strong typed programming language.

When we talk about weak typed languages, Perl is one of them. The following example shows how Perl doesn't have any problems with mixing two different types.

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

I hope you find this useful,

Thanks.

苏别ゝ 2024-07-16 15:38:23

这篇文章值得一读:http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html在研究尝试时为我清除了很多东西回答类似的问题,希望其他人也觉得有用。

强类型和弱类型:

类型系统最常见的分类方式可能是“强”
或“弱”。 这是不幸的,因为这些词几乎没有
根本没有意义。 在有限的范围内,可以比较两个
具有非常相似类型系统的语言,并将其中一种指定为具有
这两个系统中较强的一个。 除此之外,这些话没有任何意义
完全没有。

静态和动态类型

这几乎是类型系统唯一常见的分类
这具有真正的意义。 其实它的意义在于
经常被低估的[...]动态和静态类型系统是
两个完全不同的事情,其目标部分发生
重叠。

静态类型系统是编译器检查的一种机制
源代码并将标签(称为“类型”)分配给各个部分
语法,然后使用它们来推断有关程序的某些信息
行为。 动态类型系统是编译器使用的一种机制
生成代码来跟踪数据的排序(巧合的是,也
称为程序使用的“类型”)。 使用同一个词
当然,这两个系统中的“类型”并不完全是
巧合; 但最好将其理解为具有某种弱点
历史意义。 试图寻找一个
世界观中“类型”在两者中实际上意味着同一件事
系统。 事实并非如此。

显式/隐式类型:

当使用这些术语时,它们指的是
编译器将推断程序各部分的静态类型。 全部
编程语言对类型有某种形式的推理。 一些
比别人拥有更多。 ML 和 Haskell 具有隐式类型,因为没有
(或很少,取决于所使用的语言和扩展)类型
需要声明。 Java 和 Ada 具有非常明确的类型,并且
人们不断地宣告事物的类型。 以上都有
(相对地,例如与 C 和 C++ 相比)强静态类型
系统。

This article is a great read: http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html Cleared up a lot of things for me when researching trying to answer a similar question, hope others find it useful too.

Strong and Weak Typing:

Probably the most common way type systems are classified is "strong"
or "weak." This is unfortunate, since these words have nearly no
meaning at all. It is, to a limited extent, possible to compare two
languages with very similar type systems, and designate one as having
the stronger of those two systems. Beyond that, the words mean nothing
at all.

Static and Dynamic Types

This is very nearly the only common classification of type systems
that has real meaning. As a matter of fact, it's significance is
frequently under-estimated [...] Dynamic and static type systems are
two completely different things, whose goals happen to partially
overlap.

A static type system is a mechanism by which a compiler examines
source code and assigns labels (called "types") to pieces of the
syntax, and then uses them to infer something about the program's
behavior. A dynamic type system is a mechanism by which a compiler
generates code to keep track of the sort of data (coincidentally, also
called its "type") used by the program. The use of the same word
"type" in each of these two systems is, of course, not really entirely
coincidental; yet it is best understood as having a sort of weak
historical significance. Great confusion results from trying to find a
world view in which "type" really means the same thing in both
systems. It doesn't.

Explicit/Implicit Types:

When these terms are used, they refer to the extent to which a
compiler will reason about the static types of parts of a program. All
programming languages have some form of reasoning about types. Some
have more than others. ML and Haskell have implicit types, in that no
(or very few, depending on the language and extensions in use) type
declarations are needed. Java and Ada have very explicit types, and
one is constantly declaring the types of things. All of the above have
(relatively, compared to C and C++, for example) strong static type
systems.

看轻我的陪伴 2024-07-16 15:38:23

语言中的强类型/弱类型与进行类型转换的容易程度有关:

例如在 Python 中:

str = 5 + 'a' 
# would throw an error since it does not want to cast one type to the other implicitly.

与 C 语言中一样:

int a = 5;
a = 5 + 'c';
/* is fine, because C treats 'c' as an integer in this case */

因此 Python 的类型比 C 更强(从这个角度来看)。

Strong/weak typing in a language is related to how easily you can do type conversions:

For example in Python:

str = 5 + 'a' 
# would throw an error since it does not want to cast one type to the other implicitly.

Where as in C language:

int a = 5;
a = 5 + 'c';
/* is fine, because C treats 'c' as an integer in this case */

Thus Python is more strongly typed than C (from this perspective).

别挽留 2024-07-16 15:38:23

也许这可以帮助您理解强类型和弱类型......

强类型: 它会尽快检查变量的类型,通常是在编译时。 它可以防止不匹配类型之间的混合操作。

强类型编程语言是这样一种语言:

  • 所有变量(或数据类型)在编译时
  • 都是已知的 严格执行类型规则(不能使用 String
    类型规则的所有异常都会
  • 导致编译时错误

弱类型:而弱类型会尽可能晚地延迟检查系统类型,通常延迟到运行时。 在这里,您可以混合类型而无需显式转换。

“弱类型”编程语言就是一种非强类型的编程语言。

首选哪个取决于您想要什么。 对于脚本和好东西,您通常需要弱类型,因为您希望编写尽可能少的代码。 在大型程序中,强类型可以减少编译时的错误。

May be this can help you to understand Strong and Weak Typing.......

Strong typing: It checks the type of variables as soon as possible, usually at compile time. It prevents mixing operations between mismatched types.

A strong-typed programming language is one in which:

  • All variables (or data types) are known at compile time
  • There is strict enforcement of typing rules (a String can't be used
    where an Integer would be expected)
  • All exceptions to typing rules results in a compile time error

Weak Typing: While weak typing is delaying checking the types of the system as late as possible, usually to run-time. In this you can mix types without an explicit conversion.

A "weak-typed" programming language is simply one which is not strong-typed.

which is preferred depends on what you want. for scripts and good stuff you will usually want weak typing, because you want to write as much less code as possible. in big programs, strong typing can reduce errors at compile time.

云柯 2024-07-16 15:38:23

弱类型意味着您不指定变量的类型,而强类型意味着您为每个变量指定严格的类型。

每种方法都有其优点,弱类型(或通常称为动态类型)、更灵活并且需要程序员编写的代码更少。 另一方面,强类型需要开发人员做更多的工作,但作为回报,它可以在运行代码之前在编译代码时提醒您注意许多错误。 动态类型可能会延迟这些简单问题的发现,直到代码被执行。

根据手头的任务,弱类型可能比强类型更好,反之亦然,但这主要是一个品味问题。 弱类型通常用在脚本语言中,而强类型则用在大多数编译语言中。

Weak typing means that you don't specify what type a variable is, and strong typing means you give a strict type to each variable.

Each has its advantages, with weak typing (or dynamic typing, as it is often called), being more flexible and requiring less code from the programmer. Strong typing, on the other hand, requires more work from the developer, but in return it can alert you of many mistakes when compiling your code, before you run it. Dynamic typing may delay the discovery of these simple problems until the code is executed.

Depending on the task at hand, weak typing may be better than strong typing, or vice versa, but it is mostly a matter of taste. Weak typing is commonly used in scripting languages, while strong typing is used in most compiled languages.

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