ylliX - Online Advertising Network
Wrapping async-await with a completion handler in Swift

Referencing itself in a struct in Swift


It took a long time, I mean years, but it finally happened. I stumbled on a struct which had a property of the same type.


At first, it is kind of interesting that the replies property compiles fine, although it is a collection of the same type. I guess it is so because arrayโ€™s storage type is a reference type.

The simplest workaround is to use a closure for capturing the actual value.


Or we could go for using a boxed wrapper type.


Or if we prefer property wrappers, using that instead.


Then there are also options like changing the struct into class instead, but that is something to consider. Or finally, creating a

All in all, it is fascinating how something simple like this actually has a pretty complex background.

If this was helpful, please let me know on Mastodon@toomasvahter orย Twitter @toomasvahter. Feel free to subscribe to RSS feed. Thank you for reading.





Source link

Leave a Reply

Your email address will not be published. Required fields are marked *