A property is a variable attached to a class. Because there is no global scope, they are the primary vehicle for storing state in Ozark. A class can have any number of properties.
Unlike other languages, all properties are technically private (only accessible to an object of that class.) An object's state is accessed purely through methods which may make use of properties, and they are not directly accessible in any other context. However, you can mark a property readable
, writable
, or public
and then referencing methods can use the variable name (without the @
) as a getter method, setter method, or both. Methods cannot otherwise share a name with properties.
inheritance Body property @head: readable Head property @leftArm: readable Arm property @rightArm: readable Arm property @leftLeg: readable Leg property @rightLeg: readable Leg property @torso: readable Torso extension setup create Head; assign to @head; setup create Arm; assign to @leftArm; setup create Arm; assign to @rightArm; setup create Leg; assign to @leftLeg; setup create Leg; assign to @rightLeg; setup create Torso; assign to @torso; setup
Property names start with @
. This prevents naming conflicts with a method's inputs, outputs, and other variables.
Properties do not use dynamic dispatch or linearization, because they are not overridable. In a multiple inheritance situation, the property selected is the one from the first-declared inheritance, unless the object is currently stored in a pointer with one of the other inheritances' types.
inheritance Object property @coordinates: public Number, Number property @name: String? extension setup & coordinates: Number, Number, name: String assign coordinates to @coordinates assign name to @name method update latitude: Number, longitude: Number assign latitude, longitude to @coordinates print "Update complete."
Optionals
Ozark uses optionals to denote variables that are allowed not to have a value. You can read more about that in Optionals. Properties can be declared as optionals with the question mark (?
) symbol, they can be "unpacked" via with
, and they can be emptied with clear
.
Notice: *Uninitialized* and *nil* are not different concepts.
A non-optional property will cause an error if it's not set at the end of the first method called on a new object; However, an optional property that has not been set
or clear
ed will not throw an error.
inheritance Contents property @prologue: TextBlock? property @chapters: [TextBlock] property @epilogue: TextBlock? method removePrologueAndEpilogue clear @prologue clear @epilogue method asString -> printableString: implied String create block: TextBlock; setup with @prologue @prologue asString -> text block append text block append all @chapters with @epilogue @epilogue asString -> text block append text block text -> assign to printableString