第一類對象(First-class Object)在1960年由 Christopher Strachey 發明,原來稱之為第一類公民(First-class citizen),意思是然函數可以作為電腦中的第一類公民。英文中也稱之為First-class entity或First-class value。
In programming language design, a first-class citizen (also object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as a parameter, returned from a function, and assigned to a variable.
第一類對象不一定是指OOP中的對象,而是指程序中的所有實體,比如:變數、函數、陣列和字典等等。第一類對象擁有以下特徵:
Go語言中的函數打破了C語言對於函數使用的限制,將函數從二等公民提升到了一等公民的位置,每個語言對於first-class object的定義都不一樣,在Go語言中,first-class object的函數意味著: