Pitch: Property Delegates

Love this idea, and I’m excited to see it return! This revised version’s power and simplicity is admirable. The Lazy<Value> example is beautiful.

I’d have to sleep on the by and $ syntax, but if they’re not perfect, they feel close.

Though I concede its use cases are few, I’d be sorry to lose this:

  • Delegates used for properties declared within a type cannot refer to the self of their enclosing type. This eliminates some use cases (e.g., implementing a Synchronized property delegate type that uses a lock defined on the enclosing type), but simplifies the design.

Could delegates take self as an explicit argument?

class C {
    var foo: Synchronized(on: self)
}

I’m guessing the answer is “no” because self isn’t fully initialized until the property already exists. Is there a future direction for this?

Can a generic type follow by?

Given that the proposed syntax doesn’t support nested delegates, would it support writing a generic delegate-wrapping delegate? e.g.

var foo: Int by NestedDelegate(Copying(), inside: DelayedMutable())

10 Likes