[swift-evolution] stack classes

Jonathan Hull jhull at gbis.com
Fri Oct 27 09:12:08 CDT 2017


Could you explain more about your use case?  I am not sure I understand the motivation…

Also, what are you hoping to dealloc in structs?

Thanks,
Jon

> On Oct 27, 2017, at 6: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:
> 
> 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