DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Closure-Based State

Closure-Based State

Ted Neward user avatar by
Ted Neward
·
Apr. 17, 16 · Mobile Zone · Interview
Like (5)
Save
Tweet
3.53K Views

Join the DZone community and get the full member experience.

Join For Free


          ZONE:  Mobile
TLDR:   State implementation in Swift
ted.neward - Interoperability Happens 
 
   

A Closure-based State implementation in Swift.

Implementation: Swift

Implementing Closure-based State in Swift is not difficult; the languagesupports function literals as first-class citizens, and the compiler is intelligent enough to knowhow to “close over” variables referenced in the enclosing scope, so a simple implementation lookspretty straightforward:

let operation = ({ () -> ((Int) -> Int) in
    var state = 100
    let inner = { (adjust: Int) -> Int in
        state = state + adjust
        return state
    }
    return inner
})()

let result = operation(100)
print(result)

Objects

Like other strongly-typed object-oriented langauges, Swift will have a bit trickier time doing thisfor object types, since the interface needs to be known by clients ahead of time. Swift does havethe concept of a protocol, which serves much like an interface does in C# or Java (or a pure-abstractclass does in C++), and when combined with the fact that Swift allows nested classes within the scopeof a function, it seems highly likely that Swift will allow the localized definition of a class thatimplements a protocol and (given the above) uses closed-over variables for state:

// This code will fail to compile!
protocol Interface {
    func Operation(adjust: Int) -> Int;
}
let maker = { () -> Interface in
    var state = 100
    class Implementation : Interface {
        func Operation(adjust: Int) -> Int {
            state = state + adjust
            return state
        }
    }

    return Implementation()
}

Unfortunately, Swift has explicitly forbidden this, with an error message that really couldn’t be anymore clear: “Class declaration cannot close over value ‘state’ defined in outer scope.” (This is asof Swift 2.2 at this writing.) Attempts to close over reference-type instances defined in the outerscope fail with the same error message, so it appears that Swift explicitly forbids a class-orientedimplementation of this pattern.

 
Swift (programming language)

Published at DZone with permission of Ted Neward, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Database B-Tree Indexing Works
  • Federated Schema Design
  • Develop Multi-Value Applications With Modern Source Code Managers
  • Ensure API Consistency and Security With Anypoint API Governance

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo