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

目录
打赏
0
0
0
0
34
分享
相关文章
2022年终总结 | 我与CSDN的那些事,如何两个月成为一名万粉博主
Hello大家好,这里是每天都在努力学习后端的小王同学,遇见即是缘分,欢迎光临我的世界~
212 0
2022年终总结 | 我与CSDN的那些事,如何两个月成为一名万粉博主
原创案例|从街边小摊到加盟店遍布全国,这位老板做对了什么?
原创案例|从街边小摊到加盟店遍布全国,这位老板做对了什么?
131 0
原创案例|从街边小摊到加盟店遍布全国,这位老板做对了什么?
面经手册 · 第15篇《码农会锁,synchronized 解毒,剖析源码深度分析!》
这是最近我总能被问到的问题,也确实是。一个初入编程职场的新人,或是一个想重新努力学习的老司机,这也不会,那也不会,总会犯愁从哪开始。 讲道理,毕竟 Java 涉及的知识太多了,要学应该是学会学习的能力,而不是去背题、背答案,拾人牙慧是不会有太多收益的。 学习的过程要找对方法,遇到问题时最好能自己想想,你有哪些方式学会这些知识。是不感觉即使让你去百度搜,你都不知道应该拿哪个关键字搜!只能拿着问题直接找人问,这样缺少思考,缺少大脑撞南墙的过程,其实最后也很难学会。
177 0
面经手册 · 第15篇《码农会锁,synchronized 解毒,剖析源码深度分析!》
【直播回顾】云栖社区特邀专家关键:JAVA反射原理以及一些常见的应用
反射的介绍以及原理,反射的使用场景,反射样例AOP介绍,AOP原理,AOP在Spring中的使用,自己简单实现一个AOP。
1699 0
【直播回顾】云栖社区特邀专家关键:JAVA反射原理以及一些常见的应用
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等