我对类型系统的理解正确吗? [英] Is my understanding of type systems correct?

查看:124
本文介绍了我对类型系统的理解正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下语句代表我对类型系统的理解(Java系统之外的实践经验太少);请更正所有错误.

The following statements represent my understanding of type systems (which suffers from too little hands-on experience outside the Java world); please correct any errors.

静态/动态区别似乎很明确:

The static/dynamic distinction seems pretty clear-cut:

  • 静态类型语言为每个变量,字段和参数分配一种类型,并且编译器阻止在不兼容类型之间进行分配.例如:C,Java,Pascal.
  • 动态类型化的语言将变量视为可以容纳所需内容的类属垃圾箱-仅在运行时实际检查值时才检查类型(如果有的话),而不是在分配值时进行检查.例如:Smalltalk,Python,JavaScript.
  • 类型推断通过从上下文中推断类型,从而使您不必在大多数情况下都声明它们,从而使静态类型的语言看起来像动态类型的语言(并具有某些优点).时间-但是与动态语言不同,您不能例如首先使用变量保存一个字符串,然后为其分配一个整数.示例:Haskell,Scala
  • Statically typed langauges assign each variable, field and parameter a type and the compiler prevents assignments between incompatible types. Examples: C, Java, Pascal.
  • Dynamically typed languages treat variables as generic bins that can hold anything you want - types are checked (if at all) only at runtime when you actually perform operations on the values, not when you assign them. Examples: Smalltalk, Python, JavaScript.
  • Type inference allows statically typed languages to look like (and have some of the advantages of) dynamically typed ones, by inferring types from the context so that you don't have to declare them most of the time - but unlike in dynamic languages, you cannot e.g. use a variable to hold a string initially and then assign an integer to it. Examples: Haskell, Scala

对于强弱区别我不太确定,我怀疑它的定义不是很明确:

I am much less certain about the strong/weak distinction, and I suspect that it's not very clearly defined:

  • 强类型语言为每个运行时值分配一个类型,并且只允许执行针对该类型定义的操作,否则会出现明确的类型错误.
  • 弱类型语言没有运行时类型检查-如果尝试对不支持的值执行操作,则结果将不可预测.它实际上可能会做些有用的事情,但更有可能会导致数据损坏,崩溃或某些不可识别的辅助错误.
  • 似乎至少有两种不同类型的弱类型语言(或可能是连续体):
    • 在C和汇编器中,值基本上是位的存储桶,因此一切皆有可能,如果让编译器取消引用以空值结尾的字符串的前4个字节,则最好希望它导致不包含合法机器的地方代码.
    • 通常也将PHP和JavaScript视为弱类型,但不将值视为不透明的位存储桶;但是,它们将执行隐式类型转换.
    • Strongly typed languages assign each runtime value a type and only allow operations to be performed that are defined for that type, otherwise there is an explicit type error.
    • Weakly typed languages don't have runtime type checks - if you try to perform an operation on a value that it does not support, the results are unpredictable. It may actually do something useful, but more likely you'll get corrupted data, a crash, or some undecipherable secondary error.
    • There seems to be at least two different kinds of weakly typed languages (or perhaps a continuum):
      • In C and assembler, values are basically buckets of bits, so anything is possible and if you get the compiler to dereference the first 4 bytes of a null-terminated string, you better hope it leads somewhere that does not contain legal machine code.
      • PHP and JavaScript are also generally considered weakly typed, but do not consider values to be opaque bit buckets; they will, however, perform implicit type conversions.

      推荐答案

      我对强/弱区别不太确定,我怀疑它的定义不是很清楚.

      I am much less certain about the strong/weak distinction, and I suspect that it's not very clearly defined.

      您说对了:不是.

      这是类型和编程语言高级类型和编程语言的作者本杰明·皮尔斯(Benjamin C. Pierce)所说的:

      This is what Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages has to say:

      我花了几周的时间...试图弄清"强类型","静态类型","安全"等,发现它非常困难....这些术语的用法千差万别,几乎使它们无用.

      I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless.

      Luca Cardelli在他的 Typeful Programming 文章中将其定义为不存在未经检查的运行时类型错误.托尼·霍尔(Tony Hoare)称​​完全相同属性为安全".其他论文称其为类型安全"或简称为安全".

      Luca Cardelli, in his Typeful Programming article, defines it as the absence of unchecked run-time type errors. Tony Hoare calls that exact same property "security". Other papers call it "type safety" or simply "safety".

      Mark-Jason Dominus 为此写了一个经典的咆哮几年前,在comp.lang.perl.moderated新闻组上,有关Perl是否为强类型的讨论.他在这句话中说,在短短的几个小时内,他就能找到8种不同的,有时是相互矛盾的定义,这些定义主要来自诸如大学教科书或经过同行评审的论文等受尊敬的资源.特别是,这些文本中包含的示例旨在帮助学生区分强类型和弱类型的语言,根据这些示例,C是强类型的,C是弱类型的,C ++是强类型的,C ++是弱类型的,Lisp是强类型,Lisp是弱类型,Perl是强类型,Perl是弱类型. (这可以消除混乱吗?)

      Mark-Jason Dominus wrote a classic rant about this a couple of years ago on the comp.lang.perl.moderated newsgroup, in a discussion about whether or not Perl was strongly typed. In this rant he states that within just a few hours of research, he was able to find 8 different, sometimes contradictory definitions, mostly from respected sources like college textbooks or peer-reviewed papers. In particular, those texts contained examples that were meant to help the students distinguish between strongly and weakly typed languages, and according to those examples, C is strongly typed, C is weakly typed, C++ is strongly typed, C++ is weakly typed, Lisp is strongly typed, Lisp is weakly typed, Perl is strongly typed, Perl is weakly typed. (Does that clear up any confusion?)

      定义是:

      • 强键入:我的编程语言
      • 键入错误:您的编程语言
      • strongly typed: my programming language
      • weakly typed: your programming language

      这篇关于我对类型系统的理解正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆