[swift-evolution] Two-for-Tuesday: Resettable Properties

Félix Cloutier felixcca at yahoo.ca
Tue Mar 15 11:01:44 CDT 2016

Resettable properties are one of the use cases for property behaviors <https://github.com/apple/swift-evolution/blob/master/proposals/0030-property-behavior-decls.md>  There was still dissent the last time it was brought to review, so a new version is expected in the Swift 3 window.


> Le 15 mars 2016 à 09:49:21, Patterson, Jason via swift-evolution <swift-evolution at swift.org> a écrit :
> Hi all,
> Recently I noticed how `null_resettable` Objective-C properties were
> imported into Swift. To recap, a `null_resettable` property in
> Objective-C indicates that the getter returns a nonnull value, while
> the setter is nullable:
>> @property (nonatomic, nonnull, null_resettable) NSString *name;
>> foo.name = @"Bar";
>> foo.name = nil; // "resets" the property
> Currently these are imported as implicitly unwrapped optionals (var
> name: String!), which is the same as if they were `null_unspecified`.
> I believe this can be improved.
> I've drafted a proposal that would improve how these are imported. In
> a nutshell, Swift would add an extra "reset" method to the imported
> interface which would allow users to explicitly reset the property by
> name. (The above example would be imported as `var name: String; func
> resetName()`.) This would improve readability and allow the getter to
> return a non-optional value.
> That proposal is here:
> https://github.com/patters/swift-evolution/blob/master/proposals/0000-importing-null_resettable.md
> However, I then wondered if this feature of Objective-C would be
> advantageous to bring to Swift. The thought there is to allow Swift to
> declare a property getter as a non-optional type, while allowing the
> setter to take an optional type. While a syntactical change has more
> cost to Swift, the benefit may outweigh that.
> There were a few ideas here but I ultimately settled on a new `set?`
> operator. The proposal then details the usage and ramifications of
> such a change. For example, the getter would continue to return `T`
> while the type of `newValue` available in the setter becomes a `T?`.
> There's a corresponding change to willSet clauses.
> That proposal is here:
> https://github.com/patters/swift-evolution/blob/master/proposals/0000-resettable-properties.md
> I think that both of these solve the problem in two different ways and
> submit both for your discussion and consideration.
> Thanks!
> Jason Patterson
> @patters
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160315/fa5ad472/attachment.html>

More information about the swift-evolution mailing list