Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First-Class Citizen

Tags:

first-class

The definition of first-class citizen found in the wiki article says:

An object is first-class when it

  • can be stored in variables and data structures
  • can be passed as a parameter to a subroutine
  • can be returned as the result of a subroutine
  • can be constructed at run-time
  • has intrinsic identity (independent of any given name)

Can someone please explain/elaborate on the 5th requirement (in bold)? I feel that the article should have provided more details as in what sense "intrinsic identity" is capturing.

Perhaps we could use functions in Javascript and functions in C in our discussion to illustrate the 5th bullet.

I believe functions in C are second-class, whereas functions are first-class in Javascript because we can do something like the following in Javascript:

var foo = function () { console.log("Hello world"); };

, which is not permitted in C.

Again, my question is really on the 5th bullet (requirement).

like image 812
nawK Avatar asked Jul 04 '13 02:07

nawK


People also ask

What does it mean when a language treats functions as first-class citizens?

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

What is first-class citizen in Python?

First-class citizens are entities that enable support for all the operations facilitating other fellow entities. These entities are often used : while passing an argument , returning a value from function , conditional modifications & value assignment.

What is a first class?

Definition of first class (Entry 2 of 2) : the first or highest group in a classification: such as. a : the highest of usually three classes of travel accommodations. b : a class of mail that comprises letters, postcards, or matter sealed against inspection.


1 Answers

Intrinsic identity is pretty simple, conceptually. If a thing has it, its identity does not depend on something external to that thing. It can be aliased, referenced, renamed, what-have-you, but it still maintains whatever that "identity" is. People (most of them, anyway) have intrinsic identity. You are you, no matter what your name is, or where you live, or what physical transformations you may have suffered in life.

An electron, on the other hand, has no intrinsic identity. Perhaps introducing quantum mechanics here just confuses the issue, but I think it's a really fantastic example. There's no way to "tag" or "label" an electron such that we could tell the difference between it and a neighbor. If you replace one electron with another, there is absolutely no way to distinguish the old one from the new one.

Back to computers: an example of "intrinsic identity" might be the value returned by Object#hashCode() in Java, or whatever mechanism a JavaScript engine uses that permits this statement to be false:

{} === {} // false

but this to be true:

function foo () {}
var bar = foo;
var baz = bar;
baz === foo; // true
like image 79
Matt Ball Avatar answered Nov 13 '22 09:11

Matt Ball