top | item 19088350

Swift 5 Exclusivity Enforcement

115 points| return_0e | 7 years ago |swift.org

61 comments

order

kris-s|7 years ago

I put Swift into the category of "almost memory managed" languages and it's never fully clicked in my brain. Thinking about strong/weak/unowned references is more difficult in my opinion than when to malloc/free something. Following the examples in the article just reinforced that maybe Swift's approach is a little too complex.

bsaul|7 years ago

The examples are complex to follow because Swift model works just fine for all the normal cases. It only becomes problematic in some edge cases that regular programmer rarely meet.

And with all the help that the compiler now provides (even before swift 5), it's becoming really really hard to shoot yourself in the foot.

saagarjha|7 years ago

The benefit being that you only have to deal with this issue rarely, rather than all the time with manual memory management.

giornogiovanna|7 years ago

Right. This is one of the things I don't like about automatic reference counting, that it's almost a complete solution, but cycles require more thought (but less cases) to deal with than manual memory management itself.

diegoperini|7 years ago

Looking from a distance, what Swift does is actually taking best practices from modern C++ (14+) and incorporating them on the language level instead of introducing new library tools.

If you regularly code C++, the "fuck, yes!" moment is there when you learn Swift.

Teknoman117|7 years ago

And here I thought this was going to be something about enforcing some exclusivity of swift to apple devices...

jake_the_third|7 years ago

With their recent attempt at patenting programming-language concepts, you'd be excused to think so. I thought the same.

zaksoup|7 years ago

I'm not totally versed in Swift. In the example provided is it ambiguous because the closure captures `count` and count itself is being modified by the method calling the block?

I don't understand why this is an example of an unsafe operation. Wouldn't clearly defined behavior of closures clarify the "3 or 4" question?

saagarjha|7 years ago

> I don't understand why this is an example of an unsafe operation. Wouldn't clearly defined behavior of closures clarify the "3 or 4" question?

I believe that it's possible to specify this, and that's basically the behavior we had before SE-0176 was implemented. The issue with this is that it was slower for a dubious benefit (the semantics are obscure and non-obvious), so it was decided that it's just better to disallow this and get the benefit of clear behavior and better optimization opportunities, at the cost of removing this somewhat uncommon and not-all-that-hard to-rewrite-for-clarity pattern.

saagarjha|7 years ago

I'm assuming that -Ounchecked also removes the runtime diagnostic?

atrick6|7 years ago

Yes, it removes exclusivity checks and bounds checks.

hnbroseph|7 years ago

neat. maybe by swift 10 they will have windows builds and i can check it out.