[swift-evolution] Idea: Allow/require "let" in property setter name declarations

Ian Partridge ipartridge at gmail.com
Fri May 6 06:09:42 CDT 2016


Currently, the syntax for explicitly naming property setters is:

class Square {
  var sideLength: Double = 0.0

  var perimeter: Double {
    get {
      return sideLength * 4.0
    }
    set(newPerimeter) { // declares newPerimeter parameter, "let" not
allowed
      sideLength = newPerimeter / 4.0
    }
  }
}

Compare this with how extraction of associated values from enumerations
looks:

enum ServerResponse {
  case Failure(String)
  case Result(Int)
}
let response = ServerResponse.Result(404)

switch response {
case .Failure(let reason): // let is required here
  print(reason)
case .Result(let code):
  print(code)
}

For consistency, would it be better to allow/require:

class Square {
  var sideLength: Double = 0.0

  var perimeter: Double {
    get {
      return sideLength * 4.0
    }
    set(*let* newPerimeter) {  // declares newPerimeter parameter
        sideLength = newPerimeter / 4.0
    }
  }
}

The idea would apply to didSet{} and willSet{} too.

-- 
Ian Partridge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160506/1746b71e/attachment.html>


More information about the swift-evolution mailing list