Type system
Type system[edit]
A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, infer types, relieving the programmer from the need to write type annotations. The formal design and study of type systems is known as type theory.
Typed versus untyped languages[edit]
A language is typed if the specification of every operation defines types of data to which the operation is applicable, with the implication that it is not applicable to other types.[47] For example, the data represented by "this text between the quotes"
is a string, and in many programming languages dividing a number by a string has no meaning and will be rejected by the compilers. The invalid operation may be detected when the program is compiled ("static" type checking) and will be rejected by the compiler with a compilation error message, or it may be detected when the program is run ("dynamic" type checking), resulting in a run-time exception. Many languages allow a function called an exception handler to be written to handle this exception and, for example, always return "-1" as the result.
A special case of typed languages are the single-type languages. These are often scripting or markup languages, such as REXX or SGML, and have only one data type—most commonly character strings which are used for both symbolic and numeric data.
In contrast, an untyped language, such as most assembly languages, allows any operation to be performed on any data, which are generally considered to be sequences of bits of various lengths.[47] High-level languages which are untyped include BCPL, Tcl, and some varieties of Forth.
In practice, while few languages are considered typed from the point of view of type theory (verifying or rejecting all operations), most modern languages offer a degree of typing.[47] Many production languages provide means to bypass or subvert the type system, trading type-safety for finer control over the program's execution (see casting).
Static versus dynamic typing[edit]
In static typing, all expressions have their types determined prior to when the program is executed, typically at compile-time. For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates.[47]
Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskelland ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.[48] Additionally, some programming languages allow for some types to be automatically converted to other types; for example, an int can be used where the program expects a float.
Dynamic typing, also called latent typing, determines the type-safety of operations at run time; in other words, types are associated with run-time values rather than textual expressions.[47] As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, potentially making debugging more difficult. Lisp, Smalltalk, Perl, Python, JavaScript, and Ruby are all examples of dynamically typed languages.
Weak and strong typing[edit]
Weak typing allows a value of one type to be treated as another, for example treating a string as a number.[47] This can occasionally be useful, but it can also allow some kinds of program faults to go undetected at compile time and even at run time.
Strong typing prevents the above. An attempt to perform an operation on the wrong type of value raises an error.[47] Strongly typed languages are often termed type-safeor safe.
An alternative definition for "weakly typed" refers to languages, such as Perl and JavaScript, which permit a large number of implicit type conversions. In JavaScript, for example, the expression 2 * x
implicitly converts x
to a number, and this conversion succeeds even if x
is null
, undefined
, an Array
, or a string of letters. Such implicit conversions are often useful, but they can mask programming errors. Strong and static are now generally considered orthogonal concepts, but usage in the literature differs. Some use the term strongly typed to mean strongly, statically typed, or, even more confusingly, to mean simply statically typed. Thus C has been called both strongly typed and weakly, statically typed.[49][50]
It may seem odd to some professional programmers that C could be "weakly, statically typed". However, notice that the use of the generic pointer, the void* pointer, does allow for casting of pointers to other pointers without needing to do an explicit cast. This is extremely similar to somehow casting an array of bytes to any kind of datatype in C without using an explicit cast, such as (int)
or (char)
.
https://en.wikipedia.org/wiki/Programming_language
Type system的更多相关文章
- Beginning Scala study note(8) Scala Type System
1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...
- Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c
错误: Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, V ...
- foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'
错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...
- Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...
- JsonException: Max allowed object depth reached while trying to export from type System.Single
在进行类转json字符串时,报错JsonException: Max allowed object depth reached while trying to export from type Sys ...
- Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'
[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from as ...
- The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name
可以强迫部署EntityFramework.SqlServer.dll这个文件到输出目录 找到1个老外的帖子,戳这里(本人测试无效,大家有可能试一下..) 解决方案以下: 在EF的上下文代码CS文件( ...
- 解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assemb
解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceMode ...
- (C# Debug)A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
Debug 模式下运行程序的时候,Output 窗口出来个错误“A first chance exception of type 'System.ArgumentException' occurred ...
- Unable to load type System.Data.Entity.DynamicProxiesXXXXXrequired for deserialization.
Memcache实例的Get方法时抛出了异常“Unable to load type System.Data.Entity.DynamicProxies.AdInfoItems_19CD09C8E46 ...
随机推荐
- CSS text-transform 属性 控制控制文本的大小写
值 描述 none 默认.定义带有小写字母和大写字母的标准的文本. capitalize 文本中的每个单词以大写字母开头. uppercase 定义仅有大写字母. lowercase 定义无大写字母, ...
- angular 常用写法
1.ng-repeat 数组数据中,不允许数组中有相同的两个数据,这个时候用下标去管理数据便可以解决这个问题 ng-repeat="item in list track by $index& ...
- 死磕itchat源码--__init__.py
itchat包中的__init__.py是该库的入口:在该文件中的源码如下: # -*- coding: utf-8 -*- from . import content from .core impo ...
- TensorFlow+实战Google深度学习框架学习笔记(11)-----Mnist识别【采用滑动平均,双层神经网络】
模型:双层神经网络 [一层隐藏层.一层输出层]隐藏层输出用relu函数,输出层输出用softmax函数 过程: 设置参数 滑动平均的辅助函数 训练函数 x,y的占位,w1,b1,w2,b2的初始化 前 ...
- 【JavaScript框架封装】数据类型检测模块功能封装
数据类型检测封装后的最终模块代码如下: /*数据类型检验*/ xframe.extend(xframe, { // 鸭子类型(duck typing)如果它走起路来像鸭子,叫起来也是鸭子,那么它就是鸭 ...
- 搞定PHP面试 - 运算符知识点整理
一.算术运算符 1. 概览 例子 名称 结果 $a + $b 加法 $a 和 $b 的和. $a - $b 减法 $a 和 $b 的差. $a * $b 乘法 $a 和 $b 的积. $a / $b ...
- 使用SeaJS,require加载Jquery的时候总是为null
这个问题困扰了我两天,使用别人的例子.官网down下来的example都没有问题.但是放到自己项目里就 var $=require("jquery") 为null. 后来发现,jq ...
- 关于工作,学习中定时备份的几个方法(cron,git,mail)
首先介绍一下cron这个定时备份的工具: crontab -e : 运行文字编辑器来设定时程表,内定的文字编辑器是 VI.假设你想用别的文字编辑器.则请先设定 VISUAL 环境变数来指定使用那个文字 ...
- Swift开发教程--怎样自己定义TabBarItem的图片显示
在做项目的时候,假设使用系统的UITabBarController的时候,底部的tab自己定义图片显示是蓝色和灰色的.这不是我们所想要的效果. 假设想显示自己定义的按下和弹起的图片效果.这个时候就须要 ...
- Java測试覆盖率工具----Cobertura,EclEmma
Cobertura 是一个与Junit集成的代码覆盖率測量工具 它是免费.开源的 它能够与Ant和Maven集成.也能够通过命令行调用 能够生成HTML或XML格式的报告 能够依照不同的标准对HTML ...