Safe and efficient C++ interoperability via non-escapable types and lifetimes
forums.swift.org
forums.swift.org
Meanwhile the compiler still can't compile many even mildly complex SwiftUI views and expressions with the "The compiler is unable to type-check this expression in reasonable time" error.
It does not even tell you what part of code it is having issues with so you could change it. You have to comment/uncomment blocks of code to find the problematic part.
> Meanwhile the compiler still can't compile many even mildly complex SwiftUI views and expressions with the "The compiler is unable to type-check this expression in reasonable time" error.
That seems like a bug in language design? What's the story behind that? Did they inadvertently make something that was quadratic or worse and then realized it was too late or too fundamental to fix it properly so they patched it over with an error message?
It's unfortunate but I would honestly be ok with a "strict mode" that requires extra annotations from the programmer if it would prevent this stupid error in practice.
This is what I would like to know, too, but you phrased it way better than I did.
I can imagine people start linting to require (TypeName.Membername) instead of just (.MemberName) because they hope it can speed up compilation. And at that point it again feels like a big mistake to have allowed it from the beginning.
> A different approach to type checking is required to fix it.
I agree, are they actively working on this though?
C# actually has a similar issue with lambdas passed to generic functions, for similar reasons. It's just what you get when you combine overload resolution with type inference.
Naturally there are those that would rather keep the status quo.
Lol this sounds funny, any other compilers that do this? Why does this happen exactly?
> It does not even tell you what part of code it is having issues with so you could change it. You have to comment/uncomment blocks of code to find the problematic part.
Awful.
TypeScript has a notorious "Type instantiation is excessively deep and possibly infinite" warning that can often be unsafely ignored with a `@ts-expect-error` comment
My question stands: why does this happen and how come it is still so primitive that you do not even get to know the location of the issue?
let result: Double = -(1 + 1) + -(1 + 1) - 1
> Swift 6 takes 6.2 seconds to compile this one line. Even toy languages compile equivalent expressions faster.Wild. :P
WITH THE EXCEPTION OF SWIFTUI, which relies entirely on inferring highly nested generic types, and stands out as a uniquely problematic Swift framework for that and other reasons. I don't use SwiftUI.
Using Xcode taught me to keep my expectations low for my programming environment. The compiler would straight up crash with shocking regularity. The debugger was utterly useless a huge majority of the time (it would just lose track of symbols and couldn’t inspect anything when you hit a breakpoint, every expression you would type to lldb would fail with an inscrutable error.) I had to keep a script around to blow away all of Xcode’s DerivedData and restart it, and I probably used it a half dozen times a day. It was terrible.
With Rust I’m using RustRover, which IIUC uses rust-analyzer which is part of the vanilla toolchain (ie. vim/neovim or vscode will be just as good as they can use it too), and I’m continually shocked at how well it works. I’ll be typing an expression that has an ambiguous type, and the moment I finish it with something unambiguous, the editor responds by filling in all the types around it within a second. And this is with tons of map/filter/iter expressions where the type inference is insanely complex. There’s no restarting the IDE because it’s out to lunch. There’s no expression that rust-analyzer can’t figure out but the compiler can (unlike swift which seems to have one type checker for editing and a different type checker in swiftc and they disagree constantly.) It’s fast, it’s reliable, and most importantly it’s correct.
Trust me, if you think Swift’s tooling issues are unavoidable reality, you need to see how good other languages have it.
The link kind of explains why this was a difficult problem, but I think it's worth a moment to consider why this particular difficult problem just wasn't solved. Why was Apple OK with just shrugging, eh, it's hard ?
This is a Quality of Implementation issue. Quality is truly important but because Quality is hard to turn into a metric you'll see management don't like to make it a goal, after all they can't really measure if they succeeded and so when it is competing with other things which are goals it's just abandoned.
Swift's compile time performance is obviously well documented. It's not great, and performance is definitely one of the trade-offs. SwiftUI leans heavily on the compiler and is very much at the sharp end of this. Most "normal Swift" code is by and large just fine. Of course discussions tend to spiral outwards, but stripping context and adding largely uninformed, quick judgements rarely adds value to any discussion.
The problem you describe is more with the design of SwiftUI, which makes massive, complex, extenuated use of the type system. The types quickly become too complex to reason about. It's a mess that hinders the developer instead of helping them.
Not what you're going for when you design an API. Apple really screwed this one up, though I doubt they will backtrack to fix it, so we're probably stuck with it.
As someone who has worked in both Swift and C++ I’m grateful for those that choose to make an effort in this space. And of course, just because some do doesn’t take away from others that work on SwiftUI. Both can be true at the same time.
Safe FFI with lifetime support is definitely lower priority though, given that FFI is usually in a thin layer of application code that can sort out safety itself.
Apple is aiming far higher. They want [1] the ability to seamlessly replace C++ code anywhere with Swift code, to write subclasses of C++ classes in Swift, etc. without giving up performance.
See https://github.com/swiftlang/swift-evolution/blob/main/visio...
and the video linked to in https://forums.swift.org/t/video-swift-as-c-successor-in-fou..., which shows how far they were a year ago.
[1] they likely won’t get there for all C++ code, but if they get to “with a few manual annotations” without giving up efficiency, that would be a major accomplishment.
The type inference time explosion issue is a known and well-documented flaw. Fixing it would require an overhaul of the type system, which is possible, but I’m not sure if there is appetite for it.
C++ interop is probably more important for Apple itself.
Swift compiler speed and error reporting are abysmal and improving them would have a much bigger impact. So far it's not getting any better at all.
If I could give up some level of type inferrence and get the build time from 3 minutes to 20 seconds, I would.
Swift has originally been designed as an ObjC replacement, mostly for UI glue code, with conveniences more like a scripting language.
So now they're working on making Swift support more low-level low-overhead code.
Naturally they couldn't tackle everything at once.
I take their old statements about replacing C and C++ as the same aspiration that Golang had to be a systems programming language.