Constructor Function: Swift

DZone 's Guide to

Constructor Function: Swift

A Constructor Function implementation in Swift.

· Mobile Zone ·
Free Resource

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)

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 {
            otherState = otherState + adjust
            return otherState
    return AnotherImplementation(state: s, otherState: 500)
let obj2 = creator(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()).

architecture, design patterns, factory, java, javascript, swift, xamarin

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}