Frage

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).

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top