Synthetic Composite Type Fieldstled
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
}
}Last updated