Hi, I am writing a Swift wrapper for a C-based library that handles geographic objects. In that library every geometry is represented by an opaque object, that I am supposed to destroy when I am done using it. I would like to wrap each geometry in a struct, but structs can't implement a deinit func, so I am forced to fallback to use classes as wrappers. Am I missing a workaround for this scenario?
Struct deinit: wouldn't be great?
I brought this up last year. The answer from the Swift team is that deinit does not make sense for structs since they have no identity. I think the reality is that it actually does make sense in one respect because they do have scoped lifetimes. However, a struct wrapping a resource is tricky in the current Swift language because passing the resource results in a copy of the struct which is not what you want for a resource.
What we really want is a way to express types with stack allocation and lifetime scoping using ownership and borrowing semantics similae to those of Rust. Hopefully we will see something like this in Swift down the road. It is definitely a bummer to require heap allocation and reference counting for types where this isn't strictly necessary.
Thank you anandabits for the reply. Actually in my case I see no issues in copying the resource, being it eventually just a small data structure referenced through a opaque handler, but I get your point.