[swift-evolution] [Proposal] Property behaviors

Michel Fortin michel.fortin at michelf.ca
Mon Dec 21 14:50:37 CST 2015


Le 21 déc. 2015 à 12:23, Joe Groff via swift-evolution <swift-evolution at swift.org> a écrit :

> 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`:

It's unclear to me whether you can write behaviour extensions with this new model. I'm not sure if that's really needed, but with the struct-based model, you could.


> 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 }
>   }
> }

Instead of simply writing `: T`, you could use a parameter list to give a name to that `super`-property:

	var behavior synchronizedByObject<T>(var parentProperty: T) where Self: Synchronizable {
	  get {
	    return self.withLock { return parentProperty }
	  }
	  set {
	    return self.withLock { parentProperty = newValue }
	  }
	}



-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca



More information about the swift-evolution mailing list