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

Rick Mann rmann at latencyzero.com
Tue Aug 9 15:34:38 CDT 2016


> On Aug 9, 2016, at 08:24 , Karl <razielim at gmail.com> wrote:
> 
> 
>> 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).

That still doesn't mean that someone on the outside looking in at Element sees let semantics.

-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list