[swift-users] Protocol with instance var that's set on construction, otherwise read-only

Karl razielim at gmail.com
Tue Aug 9 10:24:12 CDT 2016


> On 3 Aug 2016, at 02:01, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> It complains if I make it a let because computed properties must be var. Because it's a protocol, it can't be stored (even though it can be stored in the conforming type).
> 
> If I make it { get }, I can't set it in the extensions init() method.
> 
> I guess I could make it private set (not sure of the syntax for that), but it still doesn't have let semantics.

It sounds like you should put the calculation function in the protocol extension, the property in the protocol, and let conforming types decide how to store that property (and when to calculate it).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160809/3c2bb975/attachment.html>


More information about the swift-users mailing list