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

David Sweeris davesweeris at mac.com
Tue Aug 2 20:48:50 CDT 2016


Oh, it's a computed property! Got it, I thought you meant its value was computed in the init and never changed again.

Sent from my iPhone

> On Aug 2, 2016, at 19:01, Rick Mann <rmann at latencyzero.com> 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.
> 
>> On Aug 2, 2016, at 16:28 , David Sweeris <davesweeris at mac.com> wrote:
>> 
>> If I understand things correctly, you *can* make uuid a let because you’re allowed to set them (once) during init functions.
>> 
>> - Dave Sweeris
>> 
>>> On Aug 2, 2016, at 6:22 PM, Rick Mann via swift-users <swift-users at swift.org> wrote:
>>> 
>>> I'm trying to define a protocol that has a read-only, immutable member "uuid" that can be set in the init() method, but I'm having trouble. I have this:
>>> 
>>> protocol
>>> Element
>>> {
>>>  var uuid : { get }
>>> }
>>> 
>>> extension
>>> Element
>>> {
>>>  init(...)
>>>  {
>>>    self.uuid = ...
>>>  }
>>> }
>>> 
>>> I can't make it let, because they're computed.
>>> 
>>> I'm realizing from other stuff that I really can't have the init(...) method in the extension, anyway. But I'd really like to be able to specify a let member in the protocol. What's the best way to have that effect?
>>> 
>>> In my semantics, an Element has a uniquely-assigned uuid. It might be generated when the object is instantiated, or it might be deserialized from disk, but once that's done, it can never change. How do I express that?
>>> 
>>> Thanks,
>>> 
>>> -- 
>>> Rick Mann
>>> rmann at latencyzero.com
>>> 
>>> 
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> 
> -- 
> Rick Mann
> rmann at latencyzero.com
> 
> 


More information about the swift-users mailing list