28: Properties - royal-lang/rl GitHub Wiki
Royal supports properties which are similar to variables except for that they wrap around a getter-function and a setter-function.
Properties are declared using the prop keyword.
prop NAME
{
get { GET_EXPRESSION };
set{ SET_EXPRESSION };
set (valueName) { SET_EXPRESSION };
}
If no valueName is specified then the name of value will default to value.
Both the get and set expressions are optional but one of them must be present.
A property with both the get and set without any expressions but declared will be the same as a variable. These can be used to trigger invariants etc.
A property can also have a single variable declared within its scope which can only be accessed by its getter or setter.
Example:
private:
var string _name;
public:
prop string name
{
get { return _name; }
set
{
_name = newName;
}
}
prop string name
{
var string _name;
get { return _name; }
set
{
_name = value;
}
}
prop string name
{
var string _name;
get { return _name; }
set (newName)
{
_name = newName;
}
}
prop string name { get; set; }