Thursday, June 19, 2008

Strong Static Typing

from Strong Static Typing
C++ Primer 4/e in 2.3. Variables there is a key concept:『
C++ is a statically typed language, which means that types are checked at compile time. The process by which types are checked is referred to as type-checking.
In most languages, the type of an object constrains the operations that the object can perform. If the type does not support a given operation, then an object of that type cannot perform that operation.
In C++, whether an operation is legal or not is checked at compile time. When we write an expression, the compiler checks that the objects used in the expression are used in ways that are defined by the type of the objects. If not, the compiler generates an error message; an executable file is not produced.
As our programs, and the types we use, get more complicated, we’ll see that static type checking helps find bugs in our programs earlier. A consequence of static checking is that the type of every entity used in our programs must be known to the compiler. Hence, we must define the type of a variable before we can use that variable in our programs.』
The Chinese translation said:『C++是個靜態型別語言,意思是型別在編譯期被檢查。型別被檢查的過程稱為型別檢驗。
在大部分語言中,物件的型別限制了該物件能夠執行的操作。如果物件的型別不支援某個操作,這個物件就不能執行那項操作。
在C++中,一個操作是否合法,將在編譯期被檢查。當我們寫下一個算式,編譯器會檢查算式裡用到的物件是否按其型別所定義的方法來使用。如果不是,編譯器會產出一個錯誤訊息,不產出可執行檔。
隨著我們的程式和使用的型別愈來愈複雜,我們會看到,靜態型別檢驗有助於早期找出程式臭蟲。靜態檢驗的一個結果是,程式用到的每一個物體的型別都必須為編譯器所知。因此我們必須在程式使用某個變數之前先定義好該變數的型別。』
I coded a long time with PHP.And I think PHP has no strong static typing.So when I return to code with C or C++, I dies very much miserably.

No comments: