8 ms·
How is the memory management? That was always the downside of objective-c. Does it allow aliasing memory? If not, I don't see a huge amount that it actually im
by duaneb 10y ago
How is the memory management? That was always the downside of objective-c. Does it allow aliasing memory?
If not, I don't see a huge amount that it actually improved over C++. Why should I use swift for server stuff over c++/go/rust?
- adamnemecek 10y ago> How is the memory management? Swift has both, reference types (heap allocated, refcounted) and value types (stack allocated (mostly)). Collections are copy on write which is actually really dank. You don't really think about memory management much (compared with Obj-C). And if you do, I usually wrap it in some sort of abstraction so that I can write it and forget it. The only modern language out of the three you mention is Rust. Swift is very similar to Rust but a bit higher level (but it seems like the languages will converge, a lot of the planned feature are creeping into Rust space). Swift is also simpler, but this is due to the fact that it's for a somewhat different purpose. A lot of the concepts are the same modulo the syntax. Rust was a major source of inspiration after all. Writing it feels like writing this C++, JS, Python, Haskell hybrid. It's like the first language I actually enjoy writing (Rust is a better language but I'm not quite fluent). That being said Swift isn't perfect but I things are getting fixed really quickly.
- deleted 10y ago[deleted]
- pcwalton 10y agoI wouldn't say Swift is simpler than Rust. The complexity is just in different places. For instance, Swift leans on OO quite heavily (as it has to for compatibility with Objective-C), and as a result its typechecker is quite a bit more complex: it's a full constraint solver as opposed to Rust's typechecker, which has a much simpler "expansion/contraction" heuristic. Of course, on the other hand, Rust has the whole lifetime system that complicates things.
- Gankro 10y agoYeah totally agreed. Swift is rife with way more special behaviours/magic to try to smooth over sloppy programming and just make stuff work. There's even a standard term for pushing a problem into the compiler: compiler heroics. Where heroics fall down, the language needs extra annotations to help the compiler out (@escaping closures is the most obvious). Swift also has a lot of slightly more ergonomic but otherwise largely unnecessary features: initializers vs static funcs, guards vs ifs, throws vs returning an enum, fileprivate vs private. Moving to working on Swift from Rust has led to me really appreciating how simple a lot of Rust really is. There's a lot of "oh it's literally just X". But the lifetime thing is a huge empowerer of Rust being simpler everywhere else. It means a lot more stuff can be "just X", because X can be made safe.
- paulddraper 10y agoWhy is COW collection "dank"?
- bluejekyll 10y agoAnd is "dank" good or bad?
- steveklabnik 10y agoIt usually means "good", though some people use it in an ironic sense to imply something is bad. Kinda like how "bad" can also mean "good" in the right context, but in reverse. Here I took it to mean "good."
- paulddraper 10y agoThe definition I am aware of is undeniably negative: "disagreeably damp, musty, and typically cold"
- steveklabnik 10y agoHa! I was so used to the... one I'm used to, that I forgot this obvious one. Seems like my analogy to "bad" was appropriate. "dank" as in "dank memes" as in "a word stoners use to describe good weed" is how I'm taking it to be used here.
- paulddraper 10y agoAh, well, I wouldn't know anything about that then ;)
- smitherfield 10y agoIt comes from stoner culture, where it's a sign of high quality when the psychoactive plant matter is still damp and smelly.
- pjmlp 10y ago> If not, I don't see a huge amount that it actually improved over C++. One advantage, from the point of view of someone that likes using C++, is being a safer programming language. C++ has several features that allow to write safe code, but they only work when working solo, or in a team that accepts and makes use of modern C++. If you cannot control the team on how they write C++, or the libraries being used, than there is a high probability of the code base just look like "C compiled with C++". Of course this also applies to Go and Rust vs C++.
- duaneb 10y ago> One advantage, from the point of view of someone that likes using C++, is being a safer programming language. How so? Last I checked swift doesn't have garbage collection (but does have refcounting, which is nearly as good, but still requires modifying algorithms with cycles), and only offer syntactic sugar over existing null pointer semantics (as opposed to the memory ownership guarantees provided by rust). Isn't this the exact same feature set offered by modern C++ with owned_ptr and shared_ptr? Go + rust both have guaranteed memory safety unless you go out of your way to disable safeguards. How hard is it to shoot yourself in the foot with swift, memory wise? Do you still have to fall back on autorelease pools for tight inner loops, like in objective-c?
- seandougall 10y agoIt sounds like you're used to manual reference counting in ObjC, which genuinely is a pain. Swift (and modern ObjC) use automatic reference counting, which is much more robust and less dependent on autorelease pools. For one thing, class instances get released when they no longer have a strong reference -- so to answer your specific question, you can totally instantiate a class for just one iteration of a loop. Plus, Swift makes much better use of value types, so you're less reliant on heap allocations in the first place. I personally cringe whenever I have to go back to a GC and relinquish all control over object lifecycle.
- pjmlp 10y ago> I personally cringe whenever I have to go back to a GC and relinquish all control over object lifecycle. I think it is a matter of which language and which GC. A few do offer the possibility to control allocation, and sadly a few others that were good at it died in the late 90's.