Synthetic Composite Type Fieldstled
🚧 Status: Synthetic fields are not implemented yet.
Fields which are not stored in the composite value are synthetic, i.e., the field value is computed. Synthetic can be either read-only, or readable and writable.
Synthetic fields are declared using the synthetic keyword.
Synthetic fields are read-only when only a getter is provided.
struct Rectangle {
pub var width: Int
pub var height: Int
// Declare a synthetic field named `area`,
// which computes the area based on the `width` and `height` fields.
//
pub synthetic area: Int {
get {
return width * height
}
}
// Declare an initializer which accepts width and height.
// As `area` is synthetic and there is only a getter provided for it,
// the `area` field cannot be assigned a value.
//
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}Synthetic fields are readable and writable when both a getter and a setter is declared.
It is invalid to declare a synthetic field with only a setter.
Last updated
Was this helpful?