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

Rick Mann rmann at latencyzero.com
Tue Aug 2 18:22:26 CDT 2016


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




More information about the swift-users mailing list