Dynamic typing

A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing values have types, but variables do not; that is, a variable can refer to a value of any type. Dynamically typed languages include APLErlangGroovyJavaScriptLisp,LuaMATLABGNU OctavePerl (for user-defined types, but not built-in types), PHPPick BASICPrologPythonRubySmalltalk and Tcl.

 

Implementations of dynamically typed languages generally associate run-time objects with "tags" containing their type information. This run-time classification is then used to implement type checks and dispatch overloaded functions, but can also enable pervasive[pəˈveɪsɪv ] uses of dynamic dispatchlate binding and similar idioms[ˈidiəm ] that would be cumbersome[ˈkʌmbəsəm  笨重的] at best in a statically typed language, requiring the use of variant types or similar features.

 

More broadly, dynamic typing can improve support for dynamic programming language features, such as generating types and functionality based on run-time data. (Nevertheless, dynamically typed languages need not support any or all such features, and some dynamic programming languages are statically typed.) On the other hand, dynamic typing provides fewer a priori guarantees: a dynamically typed language accepts and attempts to execute some programs that would be ruled as invalid by a static type checker, either due to errors in the program or due to static type checking being too conservative[kənˈsɚvətɪv ].

 

Dynamic typing may result in runtime type errors—that is, at runtime, a value may have an unexpected type, and an operation nonsensical for that type is applied. Such errors may occur long after the place where the programming mistake was made—that is, the place where the wrong type of data passed into a place it should not have. This may make the bug difficult to locate.

 

posted @ 2012-06-07 11:03  Season2009  阅读(207)  评论(0编辑  收藏  举报