[swift-evolution] stack classes
John McCall
rjmccall at apple.com
Fri Oct 27 13:41:09 CDT 2017
> On Oct 27, 2017, at 9:27 AM, Mike Kluev via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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:
Move-only struct types will be able to provide deinit. See the ownership manifesto.
John.
>
> 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
> }
> }
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list