[용어] 일급 객체란 무엇인가

1 분 소요

일급 객체란 ?

  • 일급 객체는 1960년대, 영국의 과학자 Christopher Strachey가 ALGOL의 실수와 프로시저를 비교함으로써 처음 언급

    First and second class objects. In ALGOL, a real number may appear in an expression or be assigned to a variable, and either of them may appear as an actual parameter in a procedure call. A procedure, on the other hand, may only appear in another procedure call either as the operator (the most common case) or as one of the actual parameters. There are no other expressions involving procedures or whose results are procedures. Thus in a sense procedures in ALGOL are second class citizens—they always have to appear in person and can never be represented by a variable or expression (except in the case of a formal parameter)…

  • 이후, Robin Popplestone이 4가지 요소로 일급객체에 대해 정의 (모두 충족해야만 일급 객체)

    1. 모든 일급 객체는 함수의 매개변수가 될 수 있다.
    2. 모든 일급 객체는 함수의 반환값이 될 수 있다.
    3. 모든 일급 객체는 할당의 대상이 될 수 있다.
    4. 모든 일급 객체는 비교의 대상이 될 수 있다.
  • 프로그래밍 언어의 정수와 실수는 일급 객체이다.
  • 배열과 문자열은 특정 언어에서 할당 및 매개변수로 넘길 수 없어 일급 객체가 아니다.
    • 예시 ) C언어에서는 배열과 문자열은 변수 및 매개변수에 할당할 수 있는 값은 배열과 문자열 자체가 아닌 각 요소를 처음을 가리키는 포인터(주소값)이다.

일급 객체 예시 (C 언어)

  • C언어의 정수를 통해 일급 객체에 대한 4가지 요소 실제 예시를 보려고 한다.
  1. 모든 일급 객체는 함수의 매개변수, 반환 값이 될 수 있다. (1,2번 요소)

    • 4바이트 정수 자료형 Int를 함수 매개변수로 정의한 것을 볼 수 있다.(1번 요소)

    • 함수의 반환값으로 Int 자료형으로 정의한 것을 볼 수 있다.(2번 요소)

      int add(int numberOne, int numberTwo){
      	return numberOne + numberTwo
      }
      
  2. 모든 일급 객체는 할당의 대상이 될 수 있다.

    • 정수 “4”가 Int 자료형 변수 number에 할당된 것을 볼 수 있다.(3번 요소)

      int number = 4
      
  3. 모든 일급 객체는 비교의 대상이 될 수 있다.

    • 정수 “5”와 “4”에 비교 연산자를 적용한 것을 볼 수 있다.(4번 요소)

      int isEqual = ( 5 == 4 )
      int isleftBig = ( 5 > 4 )
      

참고 자료

  • https://en.wikipedia.org/wiki/First-class_citizen

카테고리:

업데이트: