Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 1.58 KB

0087-lazy-attribute.md

File metadata and controls

51 lines (34 loc) · 1.58 KB

Rename lazy to @lazy

Introduction

Make lazy declaration modifier an attribute by renaming it to @lazy. Example:

struct ResourceManager {
  @lazy var resource: NSData = loadResource()
}

Swift-evolution thread: link to the discussion thread for that proposal

Motivation

Swift's rule for attribues/keywords is that keywords usually modify type of variable; attributes do not.

lazy clearly does not modify type of its variable, it only delays side effects of its initializer. Type of resource in the example above is still NSData.

Many other similar declaration modifiers are already attributes:

  • @available,
  • @objc,
  • @nonobjc,
  • @NSCopying,
  • @NSManaged,
  • @IBOutlet, etc

Detailed design

Remove lazy keyword, add @lazy attribute and add migration rule to replace them.

Future directions

Many people look forward to "Property behaviours" proposal for Swift 4. It allows to create declaration modifiers using Swift language.

@lazy is one of the main candidates for being extracted into standard library. But to do so, we must first make it an attribute. Because it is a breaking change, the earlier, the better.

Impact on existing code

This is a breaking change, but migration is trivial.