What Are "First-Class" Objects

What are first-class objects?

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

Source.

In C++ functions themselves are not first class objects, however:

  • You can override the '()' operator making it possible to have an object function, which is first class.
  • Function pointers are first class.
  • boost bind, lambda and function do offer first class functions

In C++, classes are not first class objects but instances of those classes are. In Python both the classes and the objects are first class objects. (See this answer for more details about classes as objects).

Here is an example of Javascript first class functions:

// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
{
var deriv = function(x)
{
return ( f(x + deltaX) - f(x) )/ deltaX;
}
return deriv;
}
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0) ~> 1
// cos(pi/2) ~> 0

Source.

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:

EDIT. When one says "everything is
an object" (like in Python), does he
indeed mean that "everything is
first-class"?

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.

What is meant by 'first class object'?

To quote Wikipedia:

In computer science, a programming
language is said to support
first-class functions (or function
literal) if it treats functions as
first-class objects. Specifically,
this means that the language supports
constructing new functions during the
execution of a program, storing them
in data structures, passing them as
arguments to other functions, and
returning them as the values of other
functions.

This page also illustrates it beautifully:

Really, just like any other variable

  • A function is an instance of the Object type
  • A function can have properties and has a link back to its constructor method
  • You can store the function in a variable
  • You can pass the function as a parameter to another function
  • You can return the function from a function

also read TrayMan's comment, interesting...

What is first-class object mean in Snowflake?

"First class object" simply means that your object exists independently.

According to the answer here (What are "first class" objects?), a first class object is an object that can be "dynamically created, destroyed, [...]"

What are first-class objects in Java and C#?

The problem is that "first class object" is not a well defined concept.

The normal usage is that someone says that an "object" is a class of thing that should have all of the properties X, Y and Z. But there are other things that don't have all of those properties, but they are sort of object-ish. So we'll call the former "first class" objects and the rest not "first class" ... and may be not objects.

The problem is that there are any number of views on the properties that a thing needs to have to make it a "first class" object. And no prospect of the people with opposing views coming to a consensus. (For example, a Javascript language expert might argue strenuously that an object is only first class if it is template-based.)

The only really solid insights about "first-classness" will be those that you can glean from the respective language specifications for Java and C#. And they only really apply within the scope of the respective languages / type systems ... and not across multiple languages.

So "first class Java object" or "first class C# object" might be meaningful, but "first class object" taken out of context is not.

Well that's my opinion ...

What is a First Class type?

Usually it means instances of T can be

  • returned from functions
  • passed into functions
  • constructed at runtime

Eg functions in C are not first class types as they cannot be constructed at runtime, but they are in JavaScript.

In some specialised circumstances, for example theorem proving, it means that types themselves are first class objects. More modern literature uses 'reified types' instead to denote this to avoid such ambiguity.

Treating classes as first-class objects

For a class to be a first class object, the language needs to support doing things like allowing functions to take classes (not instances) as parameters, be able to hold classes in containers, and be able to return classes from functions.

For an example of a language with first class classes, consider Java. Any object is an instance of its class. That class is itself an instance of java.lang.Class.

functions are first class values what does this exactly mean?

It means that functions can be passed around the same way as integers, sequences, etc.

An example (although not Scala):

>>> def add2(x):
... return x + 2
...
>>> map(add2, [1, 2, 3])
[3, 4, 5]


Related Topics



Leave a reply



Submit