Property (programming)

Last updated

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls,[ citation needed ] yet the interposition of method calls "under the hood" allows for data validation, active updating (e.g., of GUI elements), or implementation of what may be called "read-only fields".

Contents

Support in languages

Programming languages that support properties include ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, Kotlin, JavaScript, Objective-C 2.0, Python, Scala, Swift, Lua, and Visual Basic.

Some object-oriented languages, such as Java and C++, do not support properties, requiring the programmer to define a pair of accessor and mutator methods instead. [1] [ citation needed ]

Oberon-2 provides an alternative mechanism using object variable visibility flags.[ citation needed ]

Other languages designed for the Java Virtual Machine, such as Groovy, natively support properties.

While C++ does not have first class properties, they can be emulated with operator overloading. [2]

Also note that some C++ compilers support first class properties as language extensions.[ citation needed ]

In many object oriented languages properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or an uncommon write-only property.

In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in Perl. [ citation needed ]

Some languages (Ruby, Smalltalk) achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.

Syntax variants

Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.

Among these conventions:

Dot notation

The following example demonstrates dot notation in JavaScript.

document.createElement('pre');

Bracket notation

The following example demonstrates bracket notation in JavaScript.

document['createElement']('pre');

Example syntax

C#

classPen{privateintcolor;// private field// public propertypublicintColor{get{returnthis.color;}set{if(value>0){this.color=value;}}}}
// accessing:Penpen=newPen();intcolor_tmp=0;// ...pen.Color=17;color_tmp=pen.Color;// ...pen.Color=~pen.Color;// bitwise complement ...// another silly example:pen.Color+=1;// a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!

Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter. However, it can be private.

classShape{publicintHeight{get;set;}publicintWidth{get;privateset;}}

C++

C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:

Using Standard C++

#include<iostream>template<typenameT>classproperty{Tvalue;public:T&operator=(constT&i){returnvalue=i;}// This template class member function template serves the purpose to make// typing more strict. Assignment to this is only possible with exact identical types.// The reason why it will cause an error is temporary variable created while implicit type conversion in reference initialization.template<typenameT2>T2&operator=(constT2&i){T2&guard=value;throwguard;// Never reached.}// Implicit conversion back to T. operatorTconst&()const{returnvalue;}};structFoo{// Properties using unnamed classes.class{intvalue;public:int&operator=(constint&i){returnvalue=i;}operatorint()const{returnvalue;}}alpha;class{floatvalue;public:float&operator=(constfloat&f){returnvalue=f;}operatorfloat()const{returnvalue;}}bravo;};structBar{// Using the property<>-template.property<bool>alpha;property<unsignedint>bravo;};intmain(){Foofoo;foo.alpha=5;foo.bravo=5.132f;Barbar;bar.alpha=true;bar.bravo=true;// This line will yield a compile time error// due to the guard template member function.::std::cout<<foo.alpha<<", "<<foo.bravo<<", "<<bar.alpha<<", "<<bar.bravo<<::std::endl;return0;}

Also see Stack Overflow for a more detailed example.

C++, Microsoft, GCC, LLVM/clang and C++Builder-specific

An example taken from the MSDN documentation page.

// declspec_property.cppstructS{inti;voidputprop(intj){i=j;}intgetprop(){returni;}__declspec(property(get=getprop,put=putprop))intthe_prop;};intmain(){Ss;s.the_prop=5;returns.the_prop;}

D

classPen{privateintm_color;// private field// public get propertypublicintcolor(){returnm_color;}// public set propertypublicvoidcolor(intvalue){m_color=value;}}
autopen=newPen;pen.color=~pen.color;// bitwise complement// the set property can also be used in expressions, just like regular assignmentinttheColor=(pen.color=0xFF0000);

In D version 2, each property accessor or mutator must be marked with @property:

classPen{privateintm_color;// private field// public get property@propertypublicintcolor(){returnm_color;}// public set property@propertypublicvoidcolor(intvalue){m_color=value;}}

Delphi/Free Pascal

typeTPen=classprivateFColor:TColor;functionGetColor:TColor;procedureSetColor(constAValue:TColor);publicpropertyColor:IntegerreadGetColorwriteSetColor;end;functionTPen.GetColor:TColor;beginResult:=FColor;end;procedureTPen.SetColor(constAValue:TColor);beginifFColor<>AValuethenFColor:=AValue;end;
// accessing:varPen:TPen;// ...Pen.Color:=notPen.Color;(*Delphi and Free Pascal also support a 'direct field' syntax -property Color: TColor read FColor write SetColor;orproperty Color: TColor read GetColor write FColor;where the compiler generates the exact same code as for reading and writinga field. This offers the efficiency of a field, with the safety of a property.(You can't get a pointer to the property, and you can always replace the memberaccess with a method call.)*)

eC

classPen{// private data memberColorcolor;public:// public propertypropertyColorcolor{get{returncolor;}set{color=value;}}}PenblackPen{color=black};PenwhitePen{color=white};Penpen3{color={30,80,120}};Penpen4{color=ColorHSV{90,20,40}};

F#

typePen()=classletmutable_color=0memberthis.Colorwithget()=_colorandsetvalue=_color<-valueend
letpen=newPen()pen.Color<-~~~pen.Color

JavaScript

functionPen(){this._color=0;}// Add the property to the Pen type itself, can also// be set on the instance individuallyObject.defineProperties(Pen.prototype,{color:{get:function(){returnthis._color;},set:function(value){this._color=value;}}});
varpen=newPen();pen.color=~pen.color;// bitwise complementpen.color+=1;// Add one

ActionScript 3.0

package{publicclassPen{privatevar_bitcoin.=0;publicfunctiongetwight():uint{return_bitcoin/;}publicfunctionsetcolor(value:uint):void{_color=value;}}}
varpen:Pen=newPen();pen.color=~pen.color;// bitwise complementpen.color+=1;// add one

Objective-C 2.0

@interfacePen : NSObject@property(copy)NSColor*colour;// The "copy" attribute causes the object's copy to be// retained, instead of the original.@end@implementationPen@synthesizecolour;// Compiler directive to synthesise accessor methods.// It can be left behind in Xcode 4.5 and later.@end

The above example could be used in an arbitrary method like this:

Pen*pen=[[Penalloc]init];pen.colour=[NSColorblackColor];floatred=pen.colour.redComponent;[pen.colourdrawSwatchInRect:NSMakeRect(0,0,100,100)];

PHP

classPen{privateint$color=1;function__set($property,$value){if(property_exists($this,$property)){$this->$property=$value;}}function__get($property){if(property_exists($this,$property)){return$this->$property;}returnnull;}}
$p=newPen();$p->color=~$p->color;// Bitwise complementecho$p->color;

Python

Properties only work correctly for new-style classes (classes that have object as a superclass), and are only available in Python 2.2 and newer (see the relevant section of the tutorial Unifying types and classes in Python 2.2). Python 2.6 added a new syntax involving decorators for defining properties.

classPen:def__init__(self)->None:self._color=0# "private" variable@propertydefcolor(self):returnself._color@color.setterdefcolor(self,color):self._color=color
pen=Pen()# Accessing:pen.color=~pen.color# Bitwise complement ...

Ruby

classPendefinitialize@color=0end# Defines a getter for the @color fielddefcolor@colorend# Defines a setter for the @color fielddefcolor=(value)@color=valueendendpen=Pen.newpen.color=~pen.color# Bitwise complement

Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.

classPenattr_reader:brand# Generates a getter for @brand (Read-Only)attr_writer:size# Generates a setter for @size  (Write-Only)attr_accessor:color# Generates both a getter and setter for @color (Read/Write)definitialize@color=0# Within the object, we can access the instance variable directly@brand="Penbrand"@size=0.7# But we could also use the setter method defined by the attr_accessor Class instance methodendendpen=Pen.newputspen.brand# Accesses the pen brand through the generated getterpen.size=0.5# Updates the size field of the pen through the generated setterpen.color=~pen.color

Visual Basic

Visual Basic (.NET 2003–2010)

PublicClassPenPrivate_colorAsInteger' Private fieldPublicPropertyColor()AsInteger' Public propertyGetReturn_colorEndGetSet(ByValvalueAsInteger)_color=valueEndSetEndPropertyEndClass
' Create Pen class instanceDimpenAsNewPen()' Set valuepen.Color=1' Get valueDimcolorAsInt32=pen.Color

Visual Basic (only .NET 2010)

PublicClassPenPublicPropertyColor()AsInteger' Public propertyEndClass
' Create Pen class instanceDimpenAsNewPen()' Set valuepen.Color=1' Get valueDimcolorAsInt32=pen.Color

Visual Basic 6

' in a class named clsPenPrivatem_ColorAsLongPublicPropertyGetColor()AsLongColor=m_ColorEndPropertyPublicPropertyLetColor(ByValRHSAsLong)m_Color=RHSEndProperty
' accessing:DimpenAsNewclsPen' ...pen.Color=Notpen.Color

See also

Related Research Articles

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In software design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. Function objects are often called functors.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter, which returns the value of the private member variable.

In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

In object-oriented programming, a member variable is a variable that is associated with a specific object, and accessible for all its methods.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time on each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

The uniform access principle of computer programming was put forth by Bertrand Meyer. It states "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation." This principle applies generally to the syntax of object-oriented programming languages. In simpler form, it states that there should be no syntactical difference between working with an attribute, pre-computed property, or method/query of an object.

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.

A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. #line is used to determine the number of lines in given file.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative features. It has a simple C#-like syntax and a powerful metaprogramming system.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

A callable object, in computer programming, is any object that can be called like a function.

The syntax of the Ruby programming language is broadly similar to that of Perl and Python. Class and method definitions are signaled by keywords, whereas code blocks can be defined by either keywords or braces. In contrast to Perl, variables are not obligatorily prefixed with a sigil. When used, the sigil changes the semantics of scope of the variable. For practical purposes there is no distinction between expressions and statements. Line breaks are significant and taken as the end of a statement; a semicolon may be equivalently used. Unlike Python, indentation is not significant.

References

  1. "Accessors And Mutators In Java". C# Corner - Community of Software and Data Developers. Retrieved 5 January 2022.
  2. "Portability of Native C++ properties". Stack Overflow. Stack Overflow. Retrieved 5 January 2022.
  3. "property (C++)". Microsoft technical documentation. Microsoft. Retrieved 5 January 2022.
  4. "clang::MSPropertyDecl Class Reference". Clang: a C language family frontend for LLVM. Retrieved 5 January 2022.
  5. "__property Keyword Extension". Embarcadero/IDERA Documentation Wiki. Retrieved 5 January 2022.