问题:什么是“一流”物品?

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

编辑。当一个人说“一切都是对象”时(就像在Python中一样),他的确意味着“一切都是一流的”吗?

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?

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


回答 0

简而言之,这意味着对对象的使用没有任何限制。它与任何其他对象相同。

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

根据语言,这可能意味着:

  • 可表示为匿名文字值
  • 可存储在变量中
  • 可存储在数据结构中
  • 具有固有身份(独立于任何给定名称)
  • 在与其他实体平等方面具有可比性
  • 可作为参数传递给过程/功能
  • 由于过程/功能可退还
  • 在运行时可构造
  • 可打印
  • 易读
  • 在分布式过程之间可传播
  • 在外部运行过程中可存储

来源

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

  • 您可以覆盖’()’运算符,以使它具有对象函数,这是一流的。
  • 函数指针是一流的。
  • boost bind,lambda和function确实提供了一流的功能

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

这是Javascript一流函数的示例:

// 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 ++中的函数是第二类,因为它们不能动态创建。

关于编辑:

编辑。当一个人说“一切都是对象”时(就像在Python中一样),他的确意味着“一切都是一流的”吗?

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

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

Source.

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

  • You can override the ‘()’ operator making it possible to have an object function, which is first class.
  • Function pointers are first class.
  • boost bind, lambda and function do offer first class functions

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).

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

Source.

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.

Regarding the edit:

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

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.


回答 1

“当一个人说“一切都是对象”时(就像在Python中一样),他的意思确实是“一切都是一流的”吗?”

是。

Python中的所有内容都是正确的对象。即使是其他语言中的“原始类型”也是如此。

您会发现像这样的对象2实际上具有相当丰富和复杂的界面。

>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

由于所有内容都是Python中的一流对象,因此很少有晦涩的特殊情况。

例如,在Java中,有一些原始类型(int,bool,double,char)不是适当的对象。这就是Java必须将Integer,Boolean,Double和Character作为一流类型引入的原因。对于初学者来说,这可能很难教-尚不清楚为什么原始类型和类必须同时存在。

这也意味着对象的类本身就是对象。这与C ++不同,C ++中的类在运行时并不总是有明显的存在。

对象的类型2type 'int'对象,它具有方法,属性和类型。

>>> type(2)
<class 'int'>

内置类型的类型inttype 'type'对象。这也具有方法和属性。

>>> type(type(2))
<class 'type'>

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

Yes.

Everything in Python is a proper object. Even things that are “primitive types” in other languages.

You find that an object like 2 actually has a fairly rich and sophisticated interface.

>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

Because everything’s a first-class object in Python, there are relatively few obscure special cases.

In Java, for example, there are primitive types (int, bool, double, char) that aren’t proper objects. That’s why Java has to introduce Integer, Boolean, Double, and Character as first-class types. This can be hard to teach to beginners — it isn’t obvious why both a primitive type and a class have to exist side-by-side.

It also means that an object’s class is — itself — an object. This is different from C++, where the classes don’t always have a distinct existence at run-time.

The type of 2 is the type 'int' object, which has methods, attributes, and a type.

>>> type(2)
<class 'int'>

The type of a built-in type like int is the type 'type' object. This has methods and attributes, also.

>>> type(type(2))
<class 'type'>

回答 2

“头等舱”意味着您可以按常规方式对它们进行操作。在大多数情况下,这仅意味着您可以将这些一等公民作为函数的参数传递,或从函数中返回它们。

对于对象而言,这是不言而喻的,但对于功能甚至类而言,并不总是那么明显:

void f(int n) { return n * 2; }

void g(Action<int> a, int n) { return a(n); }

// Now call g and pass f:

g(f, 10); // = 20

这是C#中的一个示例,其中函数实际上不是一流的对象。因此,以上代码使用一个小的解决方法(即称为的通用委托Action<>)将函数作为参数传递。其他语言(例如Ruby)甚至可以将类和代码块视为普通变量(或在Ruby中为常量)。

“First class” means you can operate on them in the usual manner. Most of the times, this just means you can pass these first-class citizens as arguments to functions, or return them from functions.

This is self-evident for objects but not always so evident for functions, or even classes:

void f(int n) { return n * 2; }

void g(Action<int> a, int n) { return a(n); }

// Now call g and pass f:

g(f, 10); // = 20

This is an example in C# where functions actually aren’t first-class objects. The above code therefore uses a small workaround (namely a generic delegate called Action<>) to pass a function as an argument. Other languages, such as Ruby, allow treating even classes and code blocks as normal variables (or in the case of Ruby, constants).


回答 3

《计算机程序的结构和解释》中的幻灯片2A(1986)中,引用了Christopher Stracey的话

头等公民的权利和特权:

  • 由变量命名。
  • 作为参数传递给过程。
  • 作为过程值返回。
  • 纳入数据结构

From a slide in Structure and Interpretation of Computer Programs, lecture 2A (1986), which in turns quotes Christopher Stracey:

The rights and privileges of first-class citizens:

  • To be named by variables.
  • To be passed as arguments to procedures.
  • To be returned as values of procedures.
  • To be incorporated into data structures

回答 4

国际海事组织,这是用来用自然语言描述事物的那些隐喻之一。该术语本质上用于将功能描述为第一类对象的上下文中。

如果您考虑使用面向对象的语言,我们可以为对象赋予各种功能,例如:继承,类定义,传递给代码的其他部分的能力(方法参数),存储在数据结构中的能力等。与通常不视为对象的实体相同,就像Java脚本中的函数一样,此类实体被视为第一类对象。

头等舱本质上是指不作为第二等舱(具有降级的行为)处理。本质上,嘲讽是完美的还是无法区分的。

IMO this is one of those metaphors used to describe things in a natural language. The term is essentially used in context of describing functions as first class objects.

If you consider a object oriented language, we can impart various features to objects for eg: inheritance, class definition, ability to pass to other sections of code(method arguments), ability to store in a data structure etc. If we can do the same with an entity which is not normally considered as a object, like functions in the case of java script, such entities are considered to be first class objects.

First class essentially here means, not handled as second class (with degraded behaviour). Essentially the mocking is perfect or indistinguishable.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。