Improving the usability of C libraries in Swift
Posted by timsneath 1 day ago
Comments
Comment by skrrtww 1 day ago
I can't help but feel that Swift will ultimately be the "slow and steady wins the race" safe language of the future. Swift steadily working "first" on both tooling and cohabitability with existing ecosystems is a huge boon for adoption. It understands what an ABI is! If I were doing a greenfield cross platform application I think Swift would be the first thing I reach for now.
The qualms I have with Swift are mostly some of the more recent complex language features that can make Swift code much harder to understand and read, as well as the brainpower required to use Swift concurrency. That and some performance concerns, though many of those seem like they may be solvable with optimizations in LLVM.
Comment by mpweiher 1 day ago
This isn't recent. The approach that Swift took had this path locked in from the start, the (d)evolution towards ever more spiraling complexity was inevitable from the initial choices.
And this is not 20/20 hindsight, a lot of people, including yours truly, were saying that fron the very start. As an example, take initialization:
2014:
https://blog.metaobject.com/2014/06/remove-features-for-grea...
The swift book has 16 rules and 14 pages just on object initialization. Chris replied in the comments: "the complexity is necessary for <feature we want> and thus simplicity must give way". My reply: "the <feature you want> is incompatible with simplicity and thus must give way".
2020:
called it!
https://blog.metaobject.com/2020/04/swift-initialization-swi...
---
Or the syntax:
https://blog.metaobject.com/2020/06/the-curious-case-of-swif...
→ Swift included all of Smalltalk's keyword message syntax as a special case of a special case of the method call syntax.
---
Rob Rix:
“Swift is a crescendo of special cases stopping just short of the general; the result is complexity in the semantics, complexity in the behaviour (i.e. bugs), and complexity in use (i.e. workarounds).”
https://www.quora.com/Which-features-overcomplicate-Swift-Wh...
Comment by pinnochio 1 day ago
One of the arguments for switching to Swift was that it would be easier for new programmers. Now I think it's more of a barrier than Obj-C ever was.
Comment by ben_w 23 hours ago
Swift 5 isn't that bad (even if result builders felt like a weird hack to make SwiftUI possible and I dislike SwiftUI massively) but around that point the language has increasingly made me think "why did this happen when Java already existed?"
Comment by mpweiher 1 day ago
Although more and more I am shifting to Objective-Smalltalk: https://objective.st
Comment by pinnochio 1 day ago
Unfortunately, in my place of work, going back to Obj-C isn't an option.
Comment by aaronbrethorst 1 day ago
Comment by arcticbull 1 day ago
Now some C functions which are indistinguishable from free Swift functions get named parameters, and you can switch on some enumerations from C, and some C objects are ref counted but other ones still need you to do it. It's going to be quite something to keep track of which library is which since there's no way to know apriori.
Comment by mpweiher 1 day ago
Comment by budgefrankly 1 day ago
It's quite deceptive. Rust seems initially hard to learn, but it's a small language, so you arrive at competency faster than you might think. Swift seems initially easy to learn, but is a broad language with lots of edge-cases, so you're never quite as competent as you think you are, or need to be
Comment by Pulcinella 1 day ago
Before that it just felt like what a modern OO language with reference and value types, type safety, some very light "not truly functional but nice to have" functional programming features, and readable, "normal", dot syntax would be like. The language was basically complete at that point for the purposes of writing UI apps with the existing Apple frameworks.
Comment by peterspath 1 day ago
Comment by aeontech 1 day ago
Comment by pharaohgeek 1 day ago
More support for language interoperability like this will just enhance the cross-platform experience. The Java ecosystem is what makes it so attractive to enterprises. Swift being able to easily take advantage of open-source C/C++ libraries will help with the migration.
Comment by pjmlp 1 day ago
As proven by the track record of all languages that want to be simple, created as kind of anti-trends, they always tend to evolve into complexity as their userbase grows, as it turns out other programming language didn't got complex just for fun.
Then since they were initially created as kind of anti-complexity movement, the added on features always have warts due to not wanting to break compatibility, and are only half way there.
C23 versus PL/I, ALGOL variants, Scheme R7RS (full report) vs Lisp evolution, Java 26 vs Modula-3/Eiffel, Go 1.26 versus everyone, ...
Comment by zozbot234 1 day ago
Rust understands the C ABI, and that's plenty good enough for now. It's hard to guarantee safety anyway when you're linking to what's effectively outside code (not part of the same build) because we don't really have a fully typed equivalent for raw assembly or binary output (unlike your "safe" VM's, where the bytecode always undergoes sanity checks prior to execution) - hence why the raw C ABI often suffices in a practical sense.
Comment by andeee23 1 day ago
just a few weeks ago i was trying to work on a swift project in neovim and found the whole langserver experience pretty bad
and it’s way worse when working on swif ui apps, but i guess that’s more of an apple wanting you to use xcode thing.
i wish there was better tooling, i like the language, but i just switched to nim for my side project
Comment by pentamassiv 1 day ago
Comment by pharaohgeek 1 day ago
Comment by worldsavior 1 day ago
Comment by w10-1 1 day ago
But in my experience, there are sharp cliffs whenever you get off the happy path shown in the demos. That's not a problem with code where you can design workarounds, but when you wrap highly complex (if not arcane) C API, you often can't change or omit portions of the API causing problems. So while usability may be better, apinotes might not be enough to complete the work.
If you're wrapping something, I would recommend cataloging and then verifying all the language features you need to make it work before getting too far in.
Comment by dagmx 1 day ago
I disagree. I think it’s more that it reduces the burden to port to swift. Of course there’s some stuff you’ll never be able to port because of external factors, but reducing the burden to introduce a language is the first step in allowing more stuff to be shifted to that language transparently.
Comment by nicoburns 1 day ago
Comment by gregoriol 1 day ago
Comment by handstitched 1 day ago
Comment by krzat 1 day ago
Swift Package Manager handles Swift, ObjC, C, C++ in the same project, code completion works just fine. Overall much nicer than in other ecosystems.
Comment by waffletower 1 day ago
Comment by isodev 1 day ago
Comment by woadwarrior01 1 day ago
Comment by randomNumber7 1 day ago
UnsafeMutablePointer, UnsafePointer, UnsafeMutableBufferPointer, UnsafeBufferPointer, UnsafeMutableRawPointer, UnsafeRawPointer, UnsafeMutableRawBufferPointer, UnsafeRawBufferPointer
?? This is comical and the only reason to make it this clunky is because "unsafe is bad" and you don't want people to use it.
Comment by zffr 1 day ago
- Mutable or not
- Typed or Raw
- Single object, or Buffer
Given one kind of pointer, you can convert to any other kind of pointer, but you are responsible for knowing if it’s safe to do.
The API is not super intuitive, but I can see how it makes it more clear what you are doing in your code.
Comment by dagmx 1 day ago
Is it a pointer to raw memory or a pointer to a type? Does it have a known size? Should it be allowed to be changed?
These are all questions you have to answer in C but cannot without annotations or documentation. Languages with more expressive type systems need to map that ambiguity to something.
Comment by pyrolistical 21 hours ago
The human race will go extinct with the c abi still as the defacto standard at this point.
IMO any new system programming lang needs to compete over the quality of their c abi integration.
I still haven’t found a better c abi integration than zig. It can describe c funcs with higher precision than c itself.
Comment by secretsatan 1 day ago
I'd written considerable amounts of Objective-c bridging code before that.