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

David Sweeris davesweeris at mac.com
Tue Aug 2 18:28:39 CDT 2016


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



More information about the swift-users mailing list