Clever Geek Handbook
📜 ⬆️ ⬇️

First class object

First-class objects ( English first-class object , first-class entity , first-class citizen ) in the context of a particular programming language are elements that can be passed as a parameter, returned from a function, assigned to a variable [1] .

The concept of objects of the first and second classes was proposed in 1967 by Christopher Strachey in the article “Understanding Programming Languages”, where he compared the procedures of the Algol language with the real numbers , as “ second-class citizens ” subjected to social discrimination . ) [2] .

Definition

An object is called a “first class object” if it [3] [4] :

  • can be stored in a variable or data structures ;
  • may be passed to the function as an argument ;
  • can be returned from a function as a result;
  • can be created at runtime ;
  • internally self-identifying (independent of naming).

The term “object” is used here in a general sense, and is not limited to objects of a programming language . So the values ​​of the simplest data types , for example integer and float , in many languages ​​are “first class objects”.

Examples

In C and C ++, you cannot create functions at run time, so functions are not first-class objects in these languages. At the same time, pointers to a function can be passed as an argument and returned from another function, which is why functions in C ++ are sometimes called second-class objects . Nevertheless, in C ++ there is a concept of a functional object ( English function object ), which is an object of the first class and implements semantics equivalent to functions [3] .

In Smalltalk [5] , Scala , and JavaScript [6], functions (methods) and classes are first-class objects. Since the operators ( + , - ) in Smalltalk are essentially methods, they are also first-class objects.

Sample Nim code.

  # assign the procedure to a variable
 var value = proc () =
   echo "value"

 value () # procedure call
 var value2 = value
 value2 () # procedure call

 # procedure will be transferred to another
 proc two (): string =
   return "two"

 # procedure will receive another procedure
 proc wrap ( x : proc ) =
   echo "one"
   echo x ()
   echo "three"

 # calling a procedure that receives another procedure as an input
 wrap ( two )

 # procedure that returns a procedure
 proc closure ( x : int ): proc =
   proc res ( y : int ): int =
     return y * y + x
   return res

 var result = closure ( 2 ) # call the procedure that will return another procedure
 echo result ( 3 ) # call internal procedure

Notes

  1. ↑ Scott, Michael. Programming Language Pragmatics. - San Francisco, CA: Morgan Kaufmann Publishers, 2006.
  2. ↑ Rod Burstall, “Christopher Strachey — Understanding Programming Languages”, Higher-Order and Symbolic Computation 13 : 52 (2000) (unchanged) (link not available) . Date of treatment November 2, 2013. Archived on August 12, 2017.
  3. ↑ 1 2 First Class (unspecified) . C2.com (January 25, 2006). Date of treatment July 16, 2012. Archived July 16, 2012.
  4. ↑ first class object (unspecified) . Catalysoft.com. Date of treatment October 9, 2010. Archived on August 6, 2012.
  5. ↑ First class functions in many programming languages ​​(examples)
  6. ↑ Functions are first class objects in JavaScript


Source - https://ru.wikipedia.org/w/index.php?title=First_class_object&oldid=99964946


More articles:

  • 1997 Grand Prix
  • Saga (comic book)
  • Hirshenberg, Samuel
  • Peter the Great (Cognac)
  • Grigorovo (Bolkhov district)
  • Melanph
  • Third Shadow
  • Mosque Kazim Garabekir Pasha
  • Jules Gossel
  • Altman, Diy Fedorovich

All articles

Clever Geek | 2019