【原创】first-class 解惑

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

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

/** @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 上有帅哥提出了如下的疑问: 

?


1

2

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. 

时间: 2024-08-22 14:13:51

【原创】first-class 解惑的相关文章

答疑解惑:原创文章为什么没有排名?

昨晚从博客访问的来路统计看到有人搜索"原创文章为什么没有排名"这个短句进入到博客中来,于是就以此做题写篇文章,给这位朋友或者说给一些新手朋友解答一下这个问题. 先插句题外话:昨晚百度进行了一次小更新,不知道为什么博客快照突然跳到了9月9号,之前都是快照每天或隔天,不知道这次怎么快照回档了,没有找出原因来. 好了,进入正题.究竟"原创文章为什么没有排名"呢?有这个疑问的新人,估计对于SEO有一定的了解,但是比较片面.可能常听说,网站要想提高排名就必须多多原创,多多外链

为网站快照问题解惑 如何解决快照滞后问题

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 网站快照是网站权重的一种表现,当网站快照不断更新时,说明搜索引擎对你的网站足够信任,有助于提升网站权重,当快照停止或者滞后时,则会降低搜索引擎的信任度,使网站降权,所以越来越多的站长开始关注网站快照.那么网站快照为什么会不更新或者滞后呢?在这样的情况下又该怎样恢复网站快照?别急,且看中国化妆品招商网站长为您解惑. 快照不更新的原因很多,比如网

【DBA | IT人生】数据库解惑系列

数据库解惑系列 解惑一 --大数据与Oracle数据库 原创 2017-05-09 刘士明老师 Oracle微学堂 今天不发技术文章了,针对于我的学生经常问我的问题,我简单的给大家解答一下,纯粹是主观的个人观点. 1.Oracle数据库现在在市场上还有竞争力吗? 回答这个问题,首先大家要了解去IOE的概念. 它是阿里巴巴造出的概念.其本意是,在阿里巴巴的IT架构中,去掉IBM的小型机.Oracle数据库.EMC存储设备,代之以自己在开源软件基础上开发的系统.主机被X86设备所取代,传统数据库被开

Gulp入门与解惑

Gulp简介 Gulp.js 是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务.Gulp.js是基于 Node.js构建的,利用Node.js流的威力,你可以快速构建项目. 安装Gulp 1.全局安装Gulp: npm install --global gulp 2.作为项目的开发依赖(devDependencies)安装: npm install --save-dev gulp 小贴士 使用过Gulp的人会碰到一个问题,明明全局已经安装了Gulp和Gulp的插件,但是在项

The Hybrid Response(原创)

The Hybrid Response When requirements call for changed data to result in both type 1 and type 2 behavior, the solution is disarmingly simple: provide for both. This cannot be achieved with a single attribute, but it can be achieved by providing a pai

浅解逻辑分区LPAR与DLPAR(原创)

作为一个DBA,对操作系统及底层硬件的了解也许不是那么至关重要,但是越好的了解数据库所运行的环境就能越好的了解数据库.这里笔者简单介绍下LPAR与DLPAR 为什么服务器需要分区  近来,UNIX服务器的分区技术成为一个热点.分区技术在某些应用领域的确能带来实际的好处,但也不能一概而论.这些年分区技术的作用有被逐渐夸大的趋势,在有些时候还被描述成无所不能的技术,目前主流的分区技术可以分为物理分区.逻辑分区和动态逻辑分区. 分区的优点 服务器集中 服务器的集中管理和应用模式的集中可以减少总拥有成本

ETL概述(原创)

ETL概述ETL,Extraction- Transformation-Loading的缩写,即数据抽取(Extract).转换(Transform).装载(Load)的过程,它是构建数 据仓库的重要环节.ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为 企业的决策提供分析依据.ETL是BI项目重要的一个环节.通过ETL,我们可以基于源系统中的数据来生成数据仓库.ETL为我们搭建了OLTP系统和 OLAP系统之间的桥梁

制作网站原创内容的五大技巧

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 昨天写了一篇文章"原创内容才是维持网站排名不竭的动力",分析了原创内容对于网站排名的重要性,告诉大家尽可能给网站创造大量的原创内容.其实道理大家都懂,关键是对很多站长来说,天天原创不是一般的难,可能今天写了一篇原创,但明天又不知道写什么了,好比吃了上顿没下顿,总觉得自己肚子墨水太少了.其实,所谓的原创标准是对搜索引擎而言的

Linux备份全攻略(原创)

系统备份是系统管理工作中十分重要的一个环切,本文详细介绍了各种Linux系统的备份方法,相信对大家的日常管理工作有所帮助.  备份是一项重要的工作,但是很多人没有去做.一旦由于使用不当造成数据丢失,备份就成了真正的救命者.本文将讨论设置备份的策略和如何选择备份介质,并介绍用于备份的tar和cpio工具.  设置备份策略  设置备份策略和计划可以增加进行备份的可能性.备份开始前,要确定备份哪些数据.备份的频率和使用什么样的介质进行备份.备份的时候,应该进行完全备份.  备份通常安排在空闲时间进行.