[swift-evolution] [Review] SE-0030 Property Behaviors

Joe Groff jgroff at apple.com
Wed Feb 17 13:18:29 CST 2016


If anyone wants to start playing with the feature, I now have some of the core functionality working in a branch:

https://github.com/apple/swift/pull/1297

I didn't want to waste time parsing a behavior declaration syntax while we're still painting the bikeshed, so behaviors are currently exposed as protocols with extension methods following a convention:

protocol delayedImmutable {
  // Type of the property.
  associatedtype Value
  // Storage required by the property.
  var storage: Value? { get set }
}
extension delayedImmutable {
  // Implementation of the property.
  var value: Value {
    // The property can only be read after it's been initialized.
    get {
      guard let theValue = storage else {
        fatalError("delayedImmutable property read before initialization")
      }
      return theValue
    }
    
    // The property can only be written once to initialize it.
    set {
      guard storage == nil else {
        fatalError("delayedImmutable property rewritten after initialization")
      }
      storage = newValue
    }
  }

  // Initialization logic for the property storage.
  static func initStorage() -> Value? {
    return nil
  }
}

Custom accessors and initializer expression bindings aren't handled yet, but there's enough there now to implement `delayed` initialization. Here's an example test case:

https://github.com/jckarter/swift/commit/9da36f8e1e45564a61da4cfc9ed5327bf57862df

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160217/5eecaf35/attachment.html>


More information about the swift-evolution mailing list