【原创】first-class 解惑



      今天打算将 modb 中使用的基于 libevent-1.4.13 的非线程安全 api 替换成最新的基于 libevent-2.0.21 中的线程安全 api 。在研究 libevent-2.0.21 里的 event_compat.h 文件时,看到如下说明
/** @file event2/event_compat.h

  Potentially non-threadsafe versions of the functions in event.h: provided
  only for backwards compatibility.

  In the oldest versions of Libevent, event_base was not a first-class
  structure.  Instead, there was a single event base that every function
  manipulated.  Later, when separate event bases were added, the old functions
  that didn't take an event_base argument needed to work by manipulating the
  "current" event base.  This could lead to thread-safety issues, and obscure,
  hard-to-diagnose bugs.

  @deprecated All functions in this file are by definition deprecated.
 */
大致意思是:
      本文件中定义的全部函数都已过时,仅用于后向兼容老的 API,是和 event.h 中对应函数的非线程安全版本。在老版本 libevent 中,event_base 不是 first-class 结构。相对的,存在一个独立的 event base 供所有函数操作。所以当在后续使用中出现不同的 event base 被添加使用时,由于老版本中的 API 函数没有将 event_base 作为入参使用,而是通过操作名字为 "current" 的全局 event base 来工作,所以必将导致线程安全问题,以及隐晦的不易诊断的 bug。
  
上面这段说明中就提及了 first-class 这个概念。

在 stackoverflow 上有帅哥提出了如下的疑问:
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"?
发现如下几种回答供参考:

【回答A】
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
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:
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.


【回答B】
Everything in Python is a proper object. Even things that are "primitive types" in other languages.
Because everything's a first-class object in Python, there are relatively few obscure special cases.

【回答C】
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

最后在维基百科上找到如下说明。

【wikipedia上的说明】

In programming language design, a first-class citizen (also object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as a parameter, returned from a function, and assigned to a variable.

The concept of first- and second- class objects was introduced by Christopher Strachey in the 1960s.
During the 1990s, Raphael Finkel proposed definitions of second and third class values, but these definitions have not been widely adopted.

In many older languages, arrays and strings are not first-class: they cannot be assigned as objects or passed as parameters to a subroutine. 

In most languages, data types are not first-class objects, though in some object-oriented languages classes are first-class objects, and used for metaclasses.



参考:
http://stackoverflow.com/questions/245192/what-are-first-class-objects
http://en.wikipedia.org/wiki/First-class_object
http://en.wikipedia.org/wiki/First-class_function



你可能感兴趣的:(first-class)