[swift-users] Protocol with instance var that's set on construction, otherwise read-only
Jordan Rose
jordan_rose at apple.com
Tue Aug 2 21:06:55 CDT 2016
I don’t think it makes sense to do this. A protocol cannot control how a particular property is implemented (stored or computed), and any conforming type must initialize all of its stored properties before returning from its own initializer. (You can’t write an initializer in a protocol that doesn’t delegate to another initializer because you don’t know what other stored properties the conforming type might have.)
Given that the protocol can’t control how the property gets initialized, it doesn’t make sense to allow the protocol to "set the variable, but only in the initializer”.
Jordan
> On Aug 2, 2016, at 17: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.
>
>> 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 <mailto:rmann at latencyzero.com>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160802/5eff579b/attachment.html>
More information about the swift-users
mailing list