Property Functions
Learn about property functions that return values and property functions that are used in the assignment.
We'll cover the following...
Property functions that return values
As a simple example, let’s consider a rectangle struct that consists of two members:
Press + to interact
struct Rectangle {double width;double height;}void main() {}
Let’s assume that a third property of this type becomes a requirement, which should provide the area of the rectangle:
auto garden = Rectangle(10, 20);
writeln(garden.area);
One way of achieving that requirement is to define a third member:
Press + to interact
struct Rectangle {double width;double height;double area;}void main() {}
A flaw in that design is that the object may easily become inconsistent. Although rectangles must always have the invariant of “width * height == area ...