8 ms·
Swift Proposal: Move Function
- pleb_nz 4y agoWhy the word 'move'? Is there a historically, deeper or technical resin to use a word, that to me, doesn't mean what is getting used for?
- ChrisMarshallNY 4y agoI like the idea. It's a fairly fundamental change, though. I'm not a compiler person, so I don't know how much cheese it would move. It also may argue with some of the built-in optimizations. If you want to see these in action, run the symbolic debugger in release mode.
- anonymouse008 4y agoI have wasted many, many hours tracking down reference cycles, so in some sense, I welcome this change. However, the scope creep of Swift is becoming nightmarish - nothing just makes sense anymore. My hopes for Swift are that it teaches a whole generation how to program really compelling applications, full stack, in a profitable ecosystem (though open source, Swift is a 'gateway drug' to get people into the ecosystem). We will see if these decisions advance or detract from that.
- deleted 4y ago[deleted]
- pixelrevision 4y agoI had been feeling that way until I worked on a project that uses modern React and realized that a lot of other stuff is becoming this way. At least with Swift the compiler helps you along a lot and you can lookup most language concepts easily without them being so out of date. That said I really wish the tooling would keep up. Debugging stuff like SwiftUI and async/await code is really painful right now. It feels like they keep adding this stuff without updating the tools we need to have available to really support it. This is in stark contrast to what was/is available for Objective-C and UIKit with the debugger and instruments. I'm not sure it's a good tradeoff... The high level code becomes a lot easier to read and write but keeping a mental model of what's happening is a lot harder and it becomes very thorny when things go sideways.
- marcellus23 4y agoThis is my biggest problem with the state of Apple's developer ecosystem right now. The tooling is just pathetic — I love writing in Swift, UIKit, AppKit and SwiftUI, but it can really feel like the tools are fighting you. They're just not dumping nearly enough effort into making their tooling top-notch. There's an extremely stark contrast between Xcode and Android Studio any time you need to do refactoring, for example. Xcode's refactoring tools are both limited and very very finnicky (to the point of being basically useless).
- ChrisMarshallNY 4y agoXcode is a bug farm. A HUGE bug farm. Whenever it gets an update, I might as well just go out and catch a show. When I switched to an M1-based computer, the debugging became much finickier. I suspect that this is an OS problem, as opposed to an Xcode problem (because it happens with other System-calling apps), but sometimes, spun processes seem to hang, and I can bang away on "Force Quit," until I'm blue in the face. Nothing but a hardware restart (I can't do a soft one) fixes it. But Xcode brings the beast out on that one. It hangs and stutters quite frequently.
- scarface_74 4y agoI live in the Apple ecosystem as a user. But as a developer, I could not imagine tying myself to it. It would put my career too much at the whims of Apple.
- ChrisMarshallNY 4y agoWell, I have had my career pinned to it since 1986. It has not always been fun. Lots of atomic wedgies, in the recess yard, but things have worked out OK, in the long run.
- ChrisMarshallNY 4y agoI have encountered very few of these, with Swift. The reference counter seems good. I tend to declare many of my object references as weak, anyway; even if not necessary. Of course, I'm old-fashioned (and I primarily work with UIKit), so I use classes a lot. If I am doing it the "current" way, most of the program uses value semantics, so it should not be an issue (in theory, anyway).
- anonymouse008 4y agoAye - same here. Was once just a novice who had no idea how an application could hog up 62Gb of ram - and have weird changes to objects that shouldn't. Then I learned about references, and what a biological messaging system really means (thanks, Alan and NotificationCenter!).
- saagarjha 4y ago> I tend to declare many of my object references as weak, anyway; even if not necessary. Why?
- ChrisMarshallNY 4y agoBecause they don't need to be strong. I like to establish basic Quality habits. This is one. I declare object references as weak (or, sometimes -quite rarely, unowned) by default. If they need to be strong, either the compiler will let me know, when I try assigning to it, or the runtime will let me know with a bad access crash. Much quicker debugging than chasing reference loops. Crashes are actually good. I find the cause fairly quickly. I usually don't declare local scope variables weak; only ones that escape the scope. I have found that I seldom need to switch to strong. I also do other "useless" things, like setting the constant before the variable, in comparisons: "Good Grief" == whatCharlieSays I also tend to "in-name" incoming arguments, like so: func compare(thisValue inThisValue: Int, toThisValue inThatValue: Int) -> Int I also write @IBOutlets as explicit optionals, like so: @IBOutlet weak var someLabel: UILabel? as opposed to the default: @IBOutlet weak var someLabel: UILabel! This forces me to unwrap everywhere. I avoid implicit optionals like the plague. I use a lot of guards. I will unwrap implicit optionals in system-provided resources, even though it isn't necessary. I don't really care whether or not they are "necessary," or what others think of me. Every little bit helps. I'm very conscientious about the code I write. I write it very quickly, too. Once something becomes habit, it moves rapidly. "We are what we repeatedly do. Excellence then, is not an act, but a habit" - Incorrectly attributed to Aristotle
- hota_mazi 4y agoRust and Kotlin are much more advanced than Swift when it comes to writing full stack, deployable, multi platform, and performant applications...
- aaaaaaaaaaab 4y agoMove should have been the default…
- p4bl0 4y agoWhat do you mean? Like using a variable automatically sets it as unusable anymore except if you opt-in to mark it as still usable? It kinds of remind me of linear logic, but wouldn't that be a real pain to program with?
- roblabla 4y agoSo, Rust has this very awesome thing where all variables are move by default, but types may opt in to be copied by implementing the Copy trait (a trait being the Rust equivalent of a Swift protocol). This works exceedingly well. It essentially maps to what I want to do in almost all cases: Things that can trivially be copied (because they don't contain a resource) will usually work as you'd expect in any other programming language, while things that cannot (because they contain a resource like a file descriptor, or an owned pointer to some allocated memory) will indeed mark the old variable as unusable. This is a really pleasant model to work with. So much so that I miss it terribly whenever I work in another language.
- thealistra 4y agoI believe this was in the swift roadmap to have an opt-in borrow semantics for a block of code, if you want to do low level systems programming - just like rust. But I guess it didn't fit to migrate from current obj-c ecosystem so they left it for later.
- dagmx 4y agoSwift is ref counted so I’m not sure how having move semantics be the default would really work with that? If it weren’t, I’d agree, but that was one of the core tenets of the language as a successor of ObjC
- pbardea 4y agoAre there existing tools that help debug compiler time properties (e.g. reference counting) easily? I know of many interactive debugging techniques for investigating runtime errors, but when I'm faced with a compiler error it seems like the best interface I have is just an error message and then meticulously reading the code. I'd find something like that really useful for type-related errors too. Even something as simple as breakpoints and printlns let me inspect the intermediary state of these systems.
- dwaite 4y agoSince reference counting is automatic for the most part, the language tries to express the semantics of references itself - for instance, you can capture self weakly for a callback later, and have to check that you still have a valid capture before use. After that, your issues then are typically reference cycles, which can really only be found by tooling at runtime or by certain linting tools (e.g. warn that the type definitions make reference cycles possible, even if your code isn't making cycles). There are tools such as Instruments included with Xcode to help detect cycles at runtime.
- ChildOfChaos 4y agoNot sure what this means, but I'm learning Swift and SwiftUI right now, since i'm in the apple eco system and hoping i've made the right choice, because my time is fairly limited, being an adult in my late 30's and having a full time job, it does seem a bit of a mess though.
- herodotus 4y agoI have a published App written in SwiftUI and I have written many thousands of lines of Swift. But I just scratch the surface of the language, and that is OK with me. There are many parts of the language that I almost certainly do not understand. But my code works, it runs fast and it does what I want. Yes, Swift does have a large "surface area" and that is too bad. But you can get by well without bothering with many of the darker parts.
- happytoexplain 4y agoI think the fact that advanced features are available, but their existence doesn't impact most developers writing most apps, is a great thing. (Not that Swift - especially SwiftUI - is perfect.)
- livinglist 4y agoI’m learning Swift and SwiftUI recently, and man, as a Flutter dev I’m super overwhelmed by how many features Swift has compared to Dart… it has too many ways to do things (not necessarily bad ofc)
- scottiebarnes 4y agoDon't worry, most consumer facing software development is a mess, there's constantly changing frameworks and shiny new ways of doing things.
- hbn 4y agoI tried to get into learning Swift/UI several months back and was pretty disappointed how buggy the whole thing is, even when you're trying to do pretty basic things. It seems like a lot of the iOS veterans are still not interested in switching to it so who knows, maybe I'll come back to that in the future when it's actually production-ready like Apple claims it is.
- georgelyon 4y agoThe move function is not move semantics (though the latter is being discussed by the community). The main purpose of the move function is to explicitly document where performance sensitive code depends on a specific type of compiler optimization and providing diagnostics if those conditions change in a subsequent revision of the code. Swift’s focus on progressive disclosure means most Swift users won’t need to worry about this.
- tambourine_man 4y agoSwift’s focus continues to be “how do we make the compiler happy to help it emit faster code” IMO. Very much in line with Lattner's, a compiler guy par excellence, original vision. Unfortunately for me, it's not aligned with my preferences. I like small languages and frameworks that I can master and build upon. Tough luck for me, of course, who cares. But I'm not convinced Swift is also aligned with what should be the language's main purpose: build great iOS and Mac apps.
- tambourine_man 4y agoLattner wanted it all: a language great at system programming, scripting and web framework and building apps. It's very ambitious and unheard of, but so was LLVM, so who knows, maybe he was onto something. 8 years later, it's safe to say it's not the language of choice for the new kernel or driver code, not the one for the hot new web framework and it's not replacing Python. The only thing it's got going for it is that it's the only officially supported choice for making iOS apps going forward. Ultimately, no one uses Swift voluntarily, and that's a bad sign. I write all of this with sorrow, I was cheering with joy when Swift was released.
- vips7L 4y ago> a language great at system programming, scripting and web framework and building apps. Sounds like D.
- ezfe 4y ago> Ultimately, no one uses Swift voluntarily, and that's a bad sign. Not true for me, I've used it successfully for server side work. Being able to use the same language is nice since I can pull in the same routines/data structures and not re-implement them.
- jordanmorgan10 4y agoSwift epitomizes easy to pickup, difficult to master.
- shtopointo 4y agoAnd they keep moving the goalposts.
- Kukumber 4y agogood idea, but it's stupid to put it in the standard library it should be built in
- stephencanon 4y agoVery, very little of the swift surface language is not part of the standard library. "Basic" types like `Int` and operations like `==` are defined in the standard library, rather than built-in.
- Kukumber 4y agoThey are in the runtime, not in the standard library std lib means you'll have to import a module import Move move(a)
- favorited 4y agoYou are incorrect. Swift's standard library is named "Swift" – and it is automatically imported. You can refer to something in the standard library implicitly `let x = Array<Int>()` or by specifying the standard library's module `let x = Swift.Array<Swift.Int>()`.
- stephencanon 4y agoNo. The swift stdlib is the API that is available by default without any import statements (consisting of the Swift, _Concurrency, and _StringProcessing modules). The runtime is a separate library that programs do not explicitly interact with; calls into the runtime are implicitly generated by the compiler when necessary to perform tasks like allocation or retain/release.
- Kukumber 4y agoThanks for the information
- Someone 4y agoOne of the stated goals is which enforces this by causing the compiler to emit a diagnostic upon any uses that are after the move function, so they may say it will be part of the standard library, but the compiler will have to know what it does, just as a C compiler can assume what strlen (if #included from the right system header) does and inline it any way it wishes.
- usrusr 4y agoEven if we ignore ownership semantics for a moment, I find myself missing an explicit per-identifier end of scope ("undeclare") surprisingly often. The workaround is anonymous blocks, at least if you're one of those who don't like chopping up code into countless single-use functions. But that forces you to do early declaration when aiming for overlapping scopes and I really don't see a reason for this limitation. I guess the main reason for the absence of "undeclare" in most languages is that features affecting only local scope are beneath most language designers?
- a1369209993 4y ago> I guess the main reason for the absence of "undeclare" in most languages is that features affecting only local scope are beneath most language designers? Actually it's because approximately every damn compiler framework or textbook assumes that local variables go into and out of scope in a stack/LIFO order. (So if you declare `foo` and then `bar`, `foo` can't go out of scope until `bar` does, even if `foo` was a random temporary and `bar` is needed for the entire rest of the function.) And all the algorithms for working with local variables bake in this assumption, so you have to redesign everything to get back the sane semantics you get for free in assembly. At least that's my experience with implementing proper local variable scoping; YMMV.
- usrusr 4y agoRight, I completely forgot about the "stackness" of scope on the implementation side. Sides, actually, because both compile time and run time tend to be stacks. If course they'd still be stacks, just with an unavailable marker attached to the "undeclared". Fortunately, these days languages that are imperative at their core pick up "functional spillover" like that kotlin let seem to solve that nuisance-grade problem of a dangling `foo` temp just fine. (the kotlin let is basically a list transform applied to a single instance, without all the pointless ceremony of wrapping as an Optional. I wanted to like scala more than kotlin, but I've come to respect kotlin a lot, for striking a balance between "language designer high" and the pragmatic mundane that must never be allowed full reign or else we get another groovy)
- w10-1 4y agoSwift semantics can be unduly confusing and complicated. "copy on write" value semantics mean you can have two variable references to the same memory for a long time before one mutation causes the space to be copied and mutated, leaving the other unaffected. The function name "move" here is beyond terrible. It makes sense only to compiler writers, and even then it is ambiguous. The function's goal/effect is to end the lifetime of a variable binding, to essentially force the COW. (So much for scoping.) More generally, Swift grows in fits and starts mostly driven by Apple internal needs. So if Apple needs `move(x)`, you might, too. Poor Swift team: fully exposed in open-source, but driven and locked to Apple's needs. All the best to them.
- pjmlp 4y agoThey can have some beers with Microsoft, Oracle, Google, IBM language teams.
- stephencanon 4y ago> The function name "move" here is beyond terrible. It makes sense only to compiler writers, and even then it is ambiguous. These are exactly the sorts of issues that an evolution proposal is supposed to shake out. It gives users a chance to weigh in on new features, including naming, before they land in the language or standard library. The name is very much not finalized yet, and comments pointing toward a better binding for the functionality would be welcome.
- tialaramex 4y agoI'm not sure I buy the rationale for not choosing drop() here, but then I'm not a Swift programmer. It's true that this is a bunch of magic, while Rust's drop() is so non magical its implementation, which you could copy-paste if you wanted, is supplied in the documentation: pub fn drop<T>(_x: T) { } But the reason Rust programmers write drop(x) is going to be much the same as this proposal would have Swift programmers write _ = move(x) -- to explicitly tell the compiler they want to destroy x right here. So I think as a programmer the drop(x) would make sense.
- deleted 4y ago[deleted]