[Pitch] `init` accessors

If I’m understanding the pitch correctly, the newValue parameter is optional (like it is for set) so the Angle example could be:

struct Angle {
  var degrees: Double
  var radians: Double {
    init(initializes: degrees) {
      degrees = newValue * 180 / .pi
    }

    get { degrees * .pi / 180 }
    set { degrees = newValue * 180 / .pi }
  }

  // …
}

If the body of set and init are the same, maybe explicitly aliasing them or defining them together would be an option?

Eg

Aliasing:

  var radians: Double {
    init(initializes: degrees) = set

    get { degrees * .pi / 180 }
    set { degrees = newValue * 180 / .pi }
  }
  var radians: Double {
    init(initializes: degrees) {
      degrees = newValue * 180 / .pi
    }

    get { degrees * .pi / 180 }
    set = init 
  }

Defining together:

  var radians: Double {
    init(initializes: degrees)
    set {
      degrees = newValue * 180 / .pi
    }

    get { degrees * .pi / 180 }
  }

These could be Future Directions though.

8 Likes