[swift-evolution] [Proposal] Property behaviors

Joe Groff jgroff at apple.com
Mon Dec 21 12:45:36 CST 2015


> On Dec 21, 2015, at 10:28 AM, Tal Atlas <me at tal.by> wrote:
> 
> @Joe could you elaborate on the access pattern for these properties? Are you getting back a boxed object every time or is there some sort of magic going on so the caller thinks its getting the value inside the box but still has some way to access methods on the box (say reset for the lazy property)

There's no boxing, the behavior effectively just inserts itself in the property chain. It would still act as if you had defined a backing property yourself:

var _backingProperty: Behavior<T>
var property: T {
  get {
    return _backingProperty.getBehavior()
  }
  set {
    _backingProperty.setBehavior(newValue)
  }
}

It's true that, if the behavior is implemented in terms of get/set accessors, that this may introduce temporary copies while drilling down into the property. We have other accessor patterns we allow that can avoid this inefficiency that we would use in production implementations of things like `lazy` and `delayed`.

-Joe


> 
> On Mon, Dec 21, 2015 at 1:04 PM Matthew Johnson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I really like the direction this is heading Joe!  I agree it feels a lot nicer.  It seems like the right long-term solution to me.
> 
> Making behaviors an explicit construct in the language may lead to possibilities in the future that we cannot today which would not exist with the ad-hoc approach. 
> 
>> On Dec 21, 2015, at 11:23 AM, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> I played around a bit with the idea of a special behavior declaration. I think it feels a lot nicer, though it also feels like a much bigger language change if we go this route. Inside the declaration, you need to specify:
>> - what accessors the behavior supports, to be implemented by properties using the behavior,
>> - if the behavior controls storage, what that storage is, and what initialization logic it requires,
>> - if the behavior requires an initializer, and whether that initializer is used eagerly at property initialization or deferred to later, and
>> - what operations the behavior offers, if any.
>> 
>> Here's a quick sketch of how a behavior declaration could look. As a strawman, I'll use 'var behavior' as the introducer for a property behavior (leaving the door open to 'func behavior', 'struct behavior', etc. in the possible future). If you were going to reinvent computed properties from whole cloth, that might look like this:
>> 
>> var behavior computed<T> {
>>   // A computed property requires a `get` and `set` accessor.
>>   accessor get() -> T
>>   accessor set(newValue: T)
>> 
>>   // Accessors for the property
>>   get { return get() }
>>   set { set(newValue) }
>> }
>> 
>> lazy might look something like this:
>> 
>> var behavior lazy<T> {
>>   // lazy requires an initializer expression, but it isn't
>>   // used until after object initialization.
>>   deferred initializer: T
>> 
>>   // The optional storage for the property.
>>   var value: T?
>> 
>>   // Initialize the storage to nil.
>>   init() {
>>     value = nil
>>   }
>> 
>>   // Accessors for the property.
>>   mutating get {
>>     if let value = value {
>>       return value
>>     }
>>     // `initializer` is implicitly bound to the initializer expr as a
>>     // `@noescape () -> T` within the behavior's members.
>>     let initialValue = initializer()
>>     value = initialValue
>>     return initialValue
>>   }
>> 
>>   set {
>>     value = newValue
>>   }
>> 
>>   // clear() operation for the behavior.
>>   mutating func clear() {
>>     value = nil
>>   }
>> }
>> 
>> Some behaviors like `lazy` and `resettable` want to take control of the storage to manage their semantics, but many behaviors are adapters independent of how the underlying behavior behaves. These kinds of behavior are easy to compose with other behaviors and to override base class properties with. You could use inheritance-like syntax to indicate a "wrapping" behavior like this, and commandeer `super` to refer to the underlying property. For instance, `synchronized`:
>> 
>> var behavior synchronized<T>: T {
>>   get {
>>     return sync { return super }
>>   }
>>   set {
>>     return sync { return super }
>>   }
>> }
>> 
>> or `observing` didSet/willSet:
>> 
>> var behavior observing<T>: T {
>>   accessor willSet(oldValue: T, newValue: T) { }
>>   accessor didSet(oldValue: T, newValue: T) { }
>> 
>>   get { return super }
>>   set {
>>     let oldValue = super
>>     willSet(oldValue, newValue)
>>     super = newValue
>>     didSet(oldValue, newValue)
>>   }
>> }
>> 
>> If you want to refer back to the containing `self`, we could support that too, and by treating behavior functions specially we should be able to maintain coherent semantics for backreferencing value types as well. Implementing `synchronized` with a per-object lock could look like this:
>> 
>> var behavior synchronizedByObject<T>: T where Self: Synchronizable {
>>   get {
>>     return self.withLock { return super }
>>   }
>>   set {
>>     return self.withLock { return super }
>>   }
>> }
>> 
>> (though the juxtaposed meanings of `super` and `self` here are weird together…we'd probably want a better implicit binding name for the underlying property.)
>> 
>> -Joe
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151221/165a1a51/attachment.html>


More information about the swift-evolution mailing list