[swift-evolution] [Discussion] Modernizing Attribute Case and Attribute Argument Naming

Erica Sadun erica at ericasadun.com
Wed Feb 17 13:01:33 CST 2016


Updated: https://gist.github.com/erica/29c1a7fb7f49324d572f <https://gist.github.com/erica/29c1a7fb7f49324d572f>

Modernizing Attribute Case and Attribute Argument Naming

Proposal: TBD
Author(s): Erica Sadun <http://github.com/erica>
Status: TBD
Review manager: TBD
 <https://gist.github.com/erica/29c1a7fb7f49324d572f#introduction>Introduction

Two isolated instances of snake case <https://en.wikipedia.org/wiki/Snake_case> remain in the Swift attribute grammar. This proposal updates those elements to bring them into compliance with modern Swift language standards and applies a new case pattern to existing attributes.

The Swift-Evolution discussion of this topic took place in the "[Discussion] Modernizing Attribute Case and Attribute Argument Naming" thread. Hat tip to Michael Wells and Dmitri Gribenko

 <https://gist.github.com/erica/29c1a7fb7f49324d572f#motivation-and-detail-design>Motivation and Detail Design

Parts of the Swift attribute grammar retain lower snake case patterns that no longer match modern conventions, specifically the warn_unused_result attribute and the mutable_variant argument. 

Updating the argument is easy. The mutable_variant attribute argument should use lower camel case as is standard for native Swift argument labels and be renamed mutableVariant. 

If the warn_unused_result attribute is adjusted to the current lowercase convention, its renamed version becomes warnunusedresult. While this lower case pattern matches other compound attribute examples: objc, noescape, nonobjc, and noreturn, the re-engineered version of warnunusedresult looks like a continuous string of lowercase characters instead of punching clear, identifiable semantic elements. Using lowercase for complex attributes names, including future names yet to be introduced into the language becomes confusing when used with longer compound examples.

For this reason, I recommend the Swift team adopt an upper camel case convention for attributes to match the Cocoa members (UIApplicationMain, NSManaged, NSCopying, NSApplicationMain, IBAction, IBDesignable, IBInspectable, IBOutlet). This approach avoids the otherwise confusing long name issue and affects existing attributes as follows:

@Autoclosure // was @autoclosure
@Available // was @available
@ObjC // was @objc
@NoEscape // was @noescape
@NonObjC // was @nonobjc
@NoReturn // was @noreturn
@Testable // was @testable
@WarnUnusedResult // was @warn-unused-result
@Convention  // was @convention
@NoReturn // was @noreturn
In the revised design, the following example for Swift 2.2

@warn_unused_result(mutable_variant="sortInPlace")
public func sort() -> [Self.Generator.Element]
becomes

@WarnUnusedResult(mutableVariant: "sortInPlace")
public func sort() -> [Self.Generator.Element]
This revised example uses an argument colon (as proposed in "Replacing Equal Signs with Colons For Attribute Arguments") rather than the equal sign currently specified in Swift 2.2

 <https://gist.github.com/erica/29c1a7fb7f49324d572f#alternatives-considered>Alternatives Considered

The team might prefer using lower camel case @autoClosure, @available, @objC, @noEscape, @nonObjC, @noReturn, @testable, @warnUnusedResult, @convention, @noReturn but this would be out of step with non-native Swift attributes, specifically UIApplicationMain, NSManaged, NSCopying, NSApplicationMain, IBAction, IBDesignable, IBInspectable, and IBOutlet

> On Feb 17, 2016, at 11:50 AM, Shawn Erickson <shawnce at gmail.com> wrote:
> 
> "@AutoClosure, @Available, @ObjC, @NoEscape, @NonObjC, @NoReturn, @Testable, @WarnUnusedResult, @Convention, @NoReturn"
> 
> The above are much more readable to me and I believe most other humans :)
> 
> I favor the UpperCamelCase style over the lowerCamelCase style for these.
> 
> -Shawn
> 
> On Wed, Feb 17, 2016 at 10:43 AM Erica Sadun via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Do you have an alternative suggestion? I can add the following to the Alternatives Considered:
> 
> Using lowercase for attributes names becomes confusing with longer compound examples. For example `warnunusedresult`
> looks a lot like a continuous string of lowercase characters. I recommend the Swift team consider introducing upper camel case
> for attributes to match the Cocoa members (UIApplicationMain, NSManaged, NSCopying, NSApplicationMain, IBAction, IBDesignable, IBInspectable, IBOutlet) or lower
> camel case to avoid this issue:
> 
> @AutoClosure, @Available, @ObjC, @NoEscape, @NonObjC, @NoReturn, @Testable, @WarnUnusedResult, @Convention, @NoReturn
> 
> or
> 
> @autoClosure, @available, @objC, @noEscape, @nonObjC, @noReturn, @testable, @warnUnusedResult, @convention, @noReturn
> 
> -- E
> 

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


More information about the swift-evolution mailing list