[swift-evolution] [Proposal] Property behaviors

Matthew Johnson musical.matthew at mac.com
Fri Jan 15 10:51:29 CST 2016

Hi Joe,

My overall impression is that this is moving in the right direction and getting close.  It is going to be a great feature!  The declaration syntax makes is much more clear and also more scalable. 

Many good questions have already come up so I’ll just focus on things that haven’t been discussed yet.

The proposal makes it clear when an initializer is required but is a little bit less clear about when it may be left off.  Is this correct?

var [baseProp] x // no initializer, ok as long as base doesn't have init req?
var [initializerReqt] y // no initializer, error because of the initializer requirement?

Another thing that isn’t clear is what happens when a property with a behavior is set within the initializer of the containing type:

struct S {
  var [observed] s: String
  init(s: String) {
    // What happens here?  Is the behaviors “set” accessor called?  
    // This may not always be desirable, as in the case of “observed"
    self.s = s

One thought is that it might be good to allow behaviors to have `init` accessor that is used if the property is assigned by an initializer of the containing type (only the first time the property is assigned).  This would clarify what happens during initialization of the containing type and allow for different init and set code paths when necessary.   It would be distinguished from the behavior initializer by the lack of parens.  If that is too subtle we could use a different name for the initialization accessor.

This would also allow us to support a variant `delayedImmutable` that *must* be assigned during initialization of the containing type, but not necessarily during phase 1.  That behavior would facilitate maximum safety when we must pass `self` to the initializer when constructing an instance to assign to a property (not an uncommon use case).  

If the compiler could enforce slightly relaxed initialization rules that require initialization of the property before the initializer exits and before the property is read in the initializer body, but not necessarily during phase 1, then we could achieve nearly complete static safety.  The only window for error would be any uses of self that happen outside the initializer body before the property is initialized.  

The behavior might look like this:

public var behavior phase2Immutable<Value>: Value {
  private var value: Value? = nil

  get {
    guard let value = value else {
      fatalError("property accessed before being initialized")
    return value
  init {
    value = initialValue

This would be a significant improvement over delayedImmutable in many use cases IMO.


> On Jan 13, 2016, at 4:07 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> Thanks everyone for the first round of feedback on my behaviors proposal. I've revised it with the following changes:
> - Instead of relying on mapping behaviors to function or type member lookup, I've introduced a new purpose-built 'var behavior' declaration, which declares the accessor and initializer requirements and provides the storage and behavior methods of the property. I think this gives a clearer design for authoring behaviors, and allows for a more efficient and flexible implementation model.
> - I've backed off from trying to include 'let' behaviors. As many of you noted, it's better to tackle immutable computed properties more holistically than to try to backdoor them in.
> - I suggest changing the declaration syntax to use a behavior to square brackets—'var [behavior] foo'—which avoids ambiguity with destructuring 'var' bindings, and also works with future candidates for behavior decoration, particularly `subscript`.
> Here's the revised proposal:
> https://gist.github.com/jckarter/50b838e7f036fe85eaa3
> For reference, here's the previous iteration:
> https://gist.github.com/jckarter/f3d392cf183c6b2b2ac3
> Thanks for taking a look!
> -Joe
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160115/90edaca7/attachment.html>

More information about the swift-evolution mailing list