DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Constructor Function: Swift

Constructor Function: Swift

A Constructor Function implementation in Swift.

Ted Neward user avatar by
Ted Neward
·
Apr. 16, 16 · Tutorial
Like (4)
Save
Tweet
Share
5.61K Views

Join the DZone community and get the full member experience.

Join For Free

Implementation: Swift

Being a language that allows for standalone and top-level functions, Swift makes it fairly easy to implement a Constructor Function directly. We simply define a function that returns an instance of the object type desired. However, since the language is also strongly-typed (like its kin C#, Java or C++), Swift will also require some level of declaration of the “surface area” of the thingbeing returned. Most commonly this would be done via a protocol or base class:

protocol Interface {
    func Operation(adjust: Int) -> Int;
}

let creator = { (s : Int) -> Interface in
    class Implementation : Interface {
        private var state : Int
        init(state : Int) {
            self.state = state
        }
        func Operation(adjust: Int) -> Int {
            state = state + adjust
            return state
        }
    }
    return Implementation(state: s)
}
let obj = creator(100)
print(obj.Operation(100))

Swift’s ability to allow types to be declared as nested inside of functions (something that it shares with C++ and Java) provides a highly effective form of encapsulation, without access to the type declaration, no client would ever be able to construct an instance of Implementation, and therefore this type is completely, entirely, and wholly removed from the public view.

It is important to note that the Creator function need not be a name-bound closure; an explicit func implementation would work just as well, unless there is a strong desire to allow variance in the implementation of the Constructor Function at runtime. However, should that runtime-variance facility be desired, the Constructor Function must bedeclared a var and not a let:

var origCreator = creator
creator = { (s: Int) -> Interface in
    class AnotherImplementation: Interface {
        private let interface : Interface
        private var otherState : Int

        init(state: Int, otherState : Int) {
            self.interface = origCreator(state)
            self.otherState = otherState
        }

        func Operation(adjust: Int) -> Int {
            interface.Operation(adjust)
            otherState = otherState + adjust
            return otherState
        }
    }
    return AnotherImplementation(state: s, otherState: 500)
}
let obj2 = creator(100)
print(obj2.Operation(100))

Note that the original creator must be trapped into a variable so that it can be used within the new Construction Function implementation without creating an infinite recursion. This original creator should be tucked away some place hidden if the details of this multiple-step operation is to remain properly encapsulated.

Note also that because Swift is strongly-typed, the signature of the replacement Constructor Function must match that of the original exactly, preventing any additional variables/paramters being passed in to initialize the “extended” type. This usually means that when using replaceable Constructor Functions like this, the objects being createdwill be Decorators, Composites, Proxies, and the like.

Syntactically, it may be more idiomatic Swift to name the Constructor Function name to one that is upper-case-leading, rather than the typical lower-case-leading form used bylocal variables (“Creator” instead of “creator”); this is an aesthetic choice, but it certainly has a number of reasons to consider it, particularly since the Swift language eschews the use of an “allocation keyword” (a la “new” in C# or Java). Thus, using upper-case-leading names (“Creator”) will more effectively hide away the fact that thisis a Constructor Function, and not a standard constructor, from the client (let o = ObjectCreator() instead of let o = objectCreator()).

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

  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • Introduction To OpenSSH
  • A First Look at Neon
  • Host Hack Attempt Detection Using ELK

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: