[swift-evolution] [Proposal Draft] Flexible memberwise initialization

Matthew Johnson matthew at anandabits.com
Wed Dec 23 09:36:02 CST 2015


> On Dec 22, 2015, at 1:36 PM, Guillaume Lessard <glessard at tffenterprises.com> wrote:
> 
> (adding on)
> 
>> On 22 déc. 2015, at 12:02, Matthew Johnson <matthew at anandabits.com> wrote:
>> 
>> Why would you have an immutable instance member that is always going to have a constant value of 1?
> 
> That’s obviously a toy example. You can also use function calls, and that’s useful:
> 
> public struct A {
>  public let property = random()
> }
> 
> Would it make sense to allow a user in a different module write an extension that subverts the `let` by assigning via a new initializer?
> 
> extension A {
>  init(_ value: Int) {
>    self.property = value // ouch!
>  }
> }

If the property is public then maybe it would if you interpret it as a default value, yes.  It is possible to do this for var properties today.  

In any case, these rules won’t be changing.  :)

The discussion has convinced me that there is a useful distinction between the current behavior and a “default” value when it comes to `let` properties.


More information about the swift-evolution mailing list