> On Aug 2, 2016, at 19:06 , Jordan Rose <jordan_r...@apple.com> wrote:
> 
> 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”.

Really? It seems pretty natural for a conforming type to set a property once in 
the initializer, and it's immutable from then on out. I can do that quite 
cleanly with classes, but there's no way (that I know) to describe this using 
protocols. Ideally, I could just do:

protocol
Element
{
    let uuid: UUID
}

which implies that all conforming types must initialize that value on creation, 
or provide a getter with let semantics (the latter might be too easy to break, 
and could be disallowed, requiring conforming types to create storage for the 
property and set it in init()).

> 
> Jordan
> 
> 
>> On Aug 2, 2016, at 17:01, Rick Mann via swift-users <swift-users@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 <daveswee...@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@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
>>>> rm...@latencyzero.com
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> swift-users@swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>> 
>> 
>> 
>> -- 
>> Rick Mann
>> rm...@latencyzero.com
>> 
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 


-- 
Rick Mann
rm...@latencyzero.com


_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to