什么是“头等舱"?对象? [英] What are "first class" objects?

查看:93
本文介绍了什么是“头等舱"?对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定的编程语言中,什么时候将对象或其他东西称为一流",为什么?它们与没有语言的语言有什么区别?

When are objects or something else said to be "first class" in a given programming language, and why? In what do they differ from languages where they are not?

编辑.当人们说一切都是对象"(就像在Python中一样)时,他的确表示一切都是一流的"吗?

EDIT. When one says "everything is an object" (like in Python), does he indeed mean that "everything is first-class"?

推荐答案

简而言之,这意味着对该对象的使用没有任何限制.一样 任何其他物体.

In short, it means there are no restrictions on the object's use. It's the same as any other object.

第一类对象是一个可以动态创建,销毁,传递给函数,作为值返回并具有编程语言中其他变量所具有的所有权利的实体.

A first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have.

根据语言,这可以 暗示:

Depending on the language, this can imply:

  • 可表示为匿名文字值
  • 可存储在变量中
  • 可存储在数据结构中
  • 具有固有身份(独立于任何给定名称)
  • 在与其他实体平等方面具有可比性
  • 可以作为参数传递给过程/函数
  • 作为过程/函数的结果可返回
  • 在运行时可构造
  • 可打印
  • 可读
  • 在分布式进程之间可以传输
  • 可在外部运行的进程中存储
  • being expressible as an anonymous literal value
  • being storable in variables
  • being storable in data structures
  • having an intrinsic identity (independent of any given name)
  • being comparable for equality with other entities
  • being passable as a parameter to a procedure/function
  • being returnable as the result of a procedure/function
  • being constructible at runtime
  • being printable
  • being readable
  • being transmissible among distributed processes
  • being storable outside running processes

来源.

在C ++函数中,它们本身不是一流的对象,但是:

In C++ functions themselves are not first class objects, however:

  • 您可以重写'()'运算符,以使它具有对象函数,这是一流的.
  • 函数指针是一流的.
  • boost绑定,lambda和function确实提供了一流的功能

在C ++中,类不是一流的对象,而是这些类的实例.在Python中,类都是第一类对象. (有关类作为对象的更多详细信息,请参见此答案.)

In C++, classes are not first class objects but instances of those classes are. In Python both the classes and the objects are first class objects. (See this answer for more details about classes as objects).

以下是Java脚本一级函数的示例:

Here is an example of Javascript first class functions:

// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
{
    var deriv = function(x)
    { 
       return ( f(x + deltaX) - f(x) )/ deltaX;
    }
    return deriv;
}
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0)     ~> 1
// cos(pi/2)  ~> 0

来源.

不是第一类对象的实体称为第二类对象. C ++中的函数是第二类,因为它们不能动态创建.

Entities that are not first class objects are referred to as second-class objects. Functions in C++ are second class because they can't be dynamically created.

关于

编辑.当一个人说一切都是 一个对象"(例如在Python中),他是否 确实意味着一切都是 一流"?

EDIT. When one says "everything is an object" (like in Python), does he indeed mean that "everything is first-class"?

术语对象"可以宽松地使用,并不意味着是一流的.将整个概念称为一流实体"可能更有意义.但是在Python中,它们的目标是使所有东西都达到一流.我相信发表你讲话的人的意图是一流的.

The term object can be used loosely and doesn't imply being first class. And it would probably make more sense to call the whole concept 'first class entities'. But in Python they do aim to make everything first class. I believe the intent of the person who made your statement meant first class.

这篇关于什么是“头等舱"?对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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