First-class citizen (programming): Difference between revisions
mNo edit summary |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{#addbodyclass:tag_tech}} | |||
{{#addbodyclass:tag_prog}} | |||
{{programming}} | {{programming}} | ||
Line 9: | Line 10: | ||
For example, in Javascript and Python, functions are first-class objects: | 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 exist | 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". | 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") | |||
--> | |||
Line 50: | Line 55: | ||
--> | --> | ||
<!-- | <!-- | ||
'''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. | |||
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
(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)