[swift-evolution] stack classes

Mike Kluev mike.kluev at gmail.com
Fri Oct 27 08:27:07 CDT 2017


if it wasn't already discussed here is the preliminary proposal, if it was
then my +1 to the feature.

i propose we have an explicit apparatus to denote classes having stack
storage.

stack class StackObject { // guaranteed to be on stack
}

class NonStackObject { // is not guaranteed to be on stack, can be on heap
as well
}

this is for performance reasons. sometimes what we need is “structs with
deinit” and as this is not going to happen the next best thing could be
“lightweight” classes. this shall be self obvious, here are few examples:

stack class StackObject {
    var variable = 0

    func foo() {
        print(“i am ok to live on stack”)
    }
}

stack class BadObject {
    var variable = 0

    func foo() {
        DispatchQueue.main.async {  // error: can’t be a stack class
            self.variable = 1
        }
    }
}

class NonStackObject {
    …
}

foo() {
    let stackObject = StackObject()

    DispatchQueue.main.async {
        stackObject.foo()  // error: can’t use a stack object in this
context
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171027/34d68ad4/attachment.html>


More information about the swift-evolution mailing list