4

是不需要聲明變量類型(例如PHP和Perl)的語言的質量,稱爲弱打字或動態打字?我在兩個術語中都遇到了麻煩。是不需要聲明變量類型的弱語類型或動態類型示例的語言的質量

我說對了,動態/靜態類型與類型轉換有關,而弱/強類型與變量的減速有關?

+2

沒有,有一兩件事沒有用做其他。 C#並不總是要求你聲明這個類型,因爲它有「類型推斷」;即它可以通過上下文告訴類型是什麼。但它仍然是一種強類型語言。 –

回答

3

據:http://en.wikipedia.org/wiki/Type_system#Static_and_dynamic_type_checking_in_practice

Weak typing means that a language implicitly converts (or casts) types when used.

而:

A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.

所以,強/弱,靜態/動態是兩個不同的維度。語言將是強/弱的一種,也是靜態的動態之一。例如,Ruby和Javascript都是動態類型的,但是Ruby是強類型的,而Javascript是弱類型的。也就是說,在紅寶石下面的代碼給出一個錯誤:

1.9.2p290 :001 > 'a'+1 
TypeError: can't convert Fixnum into String 

而在JavaScript中,您可以:

> 'a'+1 
>> 'a1' 

因此,強類型語言要求你兩個變量轉換爲相同類型(例如使用1.to_s),而弱類型語言將嘗試使用一些額外的內置語言邏輯將這兩個變量強制轉換爲相同類型 - 在JavaScript的情況下,將任何內容與String結合它變成一個字符串值。

參見:http://www.artima.com/weblogs/viewpost.jsp?thread=7590作進一步解釋。

0

簡而言之,強類型與對象如何綁定(實質上是早期綁定與後期綁定)有關,而不是它們如何聲明它們。

讓我們在C#中說我有這個類:

public class Foo 
{ 
    public int Bar; 
    public double Baz; 
} 

而且我宣佈Foo類型的變量:

var myFoo = new Foo(); 

當我引用符,就像這樣:

foo. 

當我輸入時,Visual Studio將顯示一個包含BarBaz的列表.,因爲它已經知道myFoo包含這些成員;它是Foo的類型。這是強大的打字;這意味着如果我拼錯巴爾或巴茲,我的程序甚至不會編譯。

然而,假設我宣佈dynamic這將導致對象綁定到被推遲類型的變量,直到執行程序:

dynamic myFoo = new Foo(); 
myFoo.Grapes = 6; 

這將編譯。在程序運行之前,我不會收到錯誤;將會拋出運行時異常,因爲Foo上不存在葡萄。

0

這是一個老問題,但對於未來的讀者這篇大文章可澄清一些事情:http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html

這是一種長,但它明確地值得。

強類型:

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.

靜態和動態類型

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.

顯性/隱性類型:

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.