【原创】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.  

目录
相关文章
|
2月前
|
缓存 Cloud Native Linux
小白咋写第一篇Blog,建议像这样
小白咋写第一篇Blog,建议像这样
27 0
小白咋写第一篇Blog,建议像这样
|
1月前
|
NoSQL 安全 数据库
SpringSecurity-从入门到精通-学习完整笔记-附(完整demo源码)来自b站三更草堂老师的视频教程见下方链接
SpringSecurity-从入门到精通-学习完整笔记-附(完整demo源码)来自b站三更草堂老师的视频教程见下方链接
63 0
|
1月前
|
安全 Java 数据库连接
【Java 从入门到出门】第一章:专栏导读(强烈推荐)
【Java 从入门到出门】第一章:专栏导读(强烈推荐)
20 1
|
9月前
|
Java 对象存储
2022年Servlect第十九课——案例实战
2022年Servlect第十九课——案例实战
32 0
|
Web App开发 搜索推荐 JavaScript
程序人生 - 个人技术博客如何选择?(CSDN、博客园、简书、知乎专栏、Github Page)
程序人生 - 个人技术博客如何选择?(CSDN、博客园、简书、知乎专栏、Github Page)
938 0
程序人生 - 个人技术博客如何选择?(CSDN、博客园、简书、知乎专栏、Github Page)
|
存储 Java
慕课网 Java 零基础题解法(原创)
在Java中,应用数组和循环,实现如下功能: 1、从键盘录入学生成绩,并存放到数组中 2、统计成绩大于90分的学生人数并输出 3、求平均成绩
234 0
慕课网 Java 零基础题解法(原创)
写给小白,Network Class
本篇内容记录了 Network Class 一个插件的应用。
188 0
写给小白,Network Class
|
SQL Java 数据库
Jpa 笔记 (二)
Jpa 笔记 (二)
180 0
教你创建自己的CSDN博客专栏
教你创建自己的CSDN博客专栏
250 0
教你创建自己的CSDN博客专栏
|
弹性计算 负载均衡 关系型数据库
Class 7 参营总结与感想
Class 7 参营总结与感想