First-class citizen (programming): Difference between revisions

From Helpful
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{#addbodyclass:tag_tech}}
{{#addbodyclass:tag_prog}}
{{programming}}


{{stub}}


{{comment|(it is potentially confusing that the 'class' in first-class here refers not to OO classes but the sense of quality - first-rate, second-rate, etc.)}}


{{comment|Confusingly, the class in first-class here refers not to OO classes but the sense of quality - first-rate, second-rate, etc.)}}


First-class citizen tends to refer to a language in which a particular ''thing'' is like any other - and ''without'' any trickery.


In programming, '''First-class citizen''' has varied definitions, that vary in the details, because the most useful distinction depends on context.


For example, you can give a more useful definition when describing to a specific static language (e.g. C++), or a specific dynamic language (e.g. JS, Python),
For example, in Javascript and Python, functions are first-class objects:
or a language that cheats usefully (e.g Java).
They are part of the language's typing system, you can construct them, you can return them, you can hand them around and assign them to variables.


 
Contrast this with C++, where functions themselves are not first class objects - after compilation,  
 
they do not exist as things you can hand around and inspect,  
First-class citizen tends to refer to a language in which a particular ''thing'' is like any other, without any trickery.
 
The most common indications / things we care about are the ability to pass as an argument, returned from a function, and assigned to a variable.
 
 
'''First-class objects''' often refers to dynamic languages, constructing objects at runtime,
refers to objects that you create at runtime that you can do that (and more).
 
 
A '''First-class type''' refers to a language
 
 
 
Sometimes that gets a little more abstract. For example, 
 
For example, in Javascript and Python, functions are first-class objects:
They are part of the language's typing system, you can construct them, you can pass them, and return them.
 
In C++ functions themselves are not first class objects - after compilation, they exist not as things you can hand around and inspect,  
only really as "if you put the right things on the stack and jump here, things will happen as expected".
only really as "if you put the right things on the stack and jump here, things will happen as expected".
(...with footnotes, I know.)
(...with footnotes, I know.)




<!--
: (Languages that say "everything is an object" often mean "all objects are first-class")
-->




Raphael Finkel splits things out a bit more:
Raphael Finkel splits this particular case out out a bit more:
* first-class - An entity you can pass as a parameter, return, and assign to a variable
* first-class - An entity you can pass as a parameter, return, and assign to a variable
* second-class - an entity you can passed as a parameter, cannot returned from a function, and cannot store into a variable
* second-class - an entity you can passed as a parameter, cannot returned from a function, and cannot store into a variable
Line 44: Line 30:
(TODO: examples)
(TODO: examples)


<!--
The most common indications / things we care about are the ability to pass as an argument, returned from a function, and assigned to a variable.




For example, in python, functions as objects is considered


<!--


'''First-class type''' sometimes means the same thing, but if used more precisely means the ability to define a ''class'' at runtime.


This can be used to point out (possibly clarify, possibly confuse) that e.g. in C++, objects are are first-class objects, but classes are not first-class objects.
'''First-class objects''' often refers to dynamic languages, constructing objects at runtime,  
refers to objects that you create at runtime that you can do that (and more).




A '''First-class type''' refers to a language




In programming, '''First-class citizen''' has varied definitions, because the most useful distinction depends on context.


Languages may say "everything is an object" often mean "all objects are first-class".
For example, you can give a more useful definition when describing to a specific static language (e.g. C++),
or a specific dynamic language (e.g. JS, Python),
or a language that cheats usefully (e.g Java).


This is clearer in that 'object' is a looser term than you'ld think, in particular outside the context of a single language -- and this statement suggests a comparison


-->
-->




<!--
<!--
'''First-class type''' is sometimes meant to use first-class object, but if used more precisely means the ability to define a ''class'' at runtime.
This can be used to point out (possibly clarify, possibly confuse) that e.g. in C++, objects are are first-class objects, but classes are not.
-->


<!--
Somewhere else on this wiki we call text to be a first-class citizen in the the DOM view of XML, there meaning a text node is a specific object that has to come under an element node.
Somewhere else on this wiki we call text to be a first-class citizen in the the DOM view of XML, there meaning a text node is a specific object that has to come under an element node.



Latest revision as of 14:49, 23 April 2024

Some fragmented programming-related notes, not meant as introduction or tutorial

Data: Numbers in computers ·· Computer dates and times ·· Data structures

Wider abstractions: Programming language typology and glossary · Generics and templating ·· Some abstractions around programming · · Computational complexity theory notes · Synchronous, asynchronous · First-class citizen

Syntaxy abstractions: Constness · Memory aliasing · Binding, assignment, and such · Closures · Context manager · Garbage collection

Sharing stuff: Communicated state and calls · Locking, data versioning, concurrency, and larger-scale computing notes

Language specific: Python notes ·· C and C++ notes · Compiling and linking ·· Lua notes

Teams and products: Programming in teams, working on larger systems, keeping code healthy · Benchmarking, performance testing, load testing, stress testing, etc. · Maintainability

More applied notes: Optimized number crunching · File polling, event notification · Webdev · GUI toolkit notes

Mechanics of duct taping software together: Automation, remote management, configuration management · Build tool notes · Installers



(it is potentially confusing that the 'class' in first-class here refers not to OO classes but the sense of quality - first-rate, second-rate, etc.)


First-class citizen tends to refer to a language in which a particular thing is like any other - and without any trickery.


For example, in Javascript and Python, functions are first-class objects: They are part of the language's typing system, you can construct them, you can return them, you can hand them around and assign them to variables.

Contrast this with C++, where functions themselves are not first class objects - after compilation, they do not exist as things you can hand around and inspect, only really as "if you put the right things on the stack and jump here, things will happen as expected". (...with footnotes, I know.)



Raphael Finkel splits this particular case out out a bit more:

  • first-class - An entity you can pass as a parameter, return, and assign to a variable
  • second-class - an entity you can passed as a parameter, cannot returned from a function, and cannot store into a variable
  • third-class - an enitity you cannot pass as a parameter, cannot returned from a function, and cannot store into a variable

(TODO: examples)





https://en.wikipedia.org/wiki/First-class_citizen