News for hackers. Stuff that matters.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @02:13PM (#34343055)
I am pretty certain that most of high level use cases for Rust could be replaced with Swift, with increased developer velocity, if Swift was actually cross platform. And that's coming from a Rust fan. It will be interesting to see how effective this rewrite is.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by kitsunesoba on Wednesday January 11, 2023 @02:18PM (#34343128)
I would love to be able to write cross-platform desktops apps in Swift. The language isn't perfect of course but I enjoy it a lot more than the alternatives I've tried.

That possibility is still a ways down the road of course but Foundation getting a cross platform rewrite is a nice step in that direction.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by rattray on Wednesday January 11, 2023 @02:43PM (#34343511)
If you or others on this thread are interested in working on a Swift compiler for Windows, please reach out to me :) a good friend is hiring a team to make this happen.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Someone on Wednesday January 11, 2023 @03:09PM (#34343908)
How will that be different from Apple’s Swift toolchain for Windows (https://www.swift.org/blog/swift-on-windows/)?
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by rattray on Wednesday January 11, 2023 @04:17PM (#34344994)
https://youtu.be/bOMQiMxh5Bc?t=430

relevant listen is about 2min long

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Someone on Wednesday January 11, 2023 @05:25PM (#34345910)
To me, that sounds as if most of the work would not be on the compiler but on improving the development experience (IDE, debugging support).

On the other hand, your comment said ”working on _a_ Swift compiler for Windows”. To me, that sounded like a new compiler, not adding missing features to the existing one. Which of the two is it?

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by etse on Wednesday January 11, 2023 @02:26PM (#34343257)
How would you compare Swift with Kotlin in terms of being a good stand-in for high-level Rust?

I've not paid attention to Kotlin in a while, but it seems like there's more progress at supporting Kotlin across more platforms (Kotlin native, web?, etc). I'm curious if people feel that the language features and design are at about parity, or if one is significantly stronger/weaker.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by manmal on Wednesday January 11, 2023 @02:42PM (#34343476)
Last time I toyed with Kotlin Multiplattform (bit less than a year ago) it provided only really weird Objective-C bindings for use in iOS/macOS. Like, a statically defined List turned into an Obj-C iterator. Enum and sealed classes were quite problematic either - I honestly would not consider using KMM for anything but a PoC.

BTW, JetBrains recently sunsetted AppCode, so they are bleeding their Swift talent now. I suspect that doesn’t bode so well for Kotlin‘s Swift interop.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @02:41PM (#34343460)
I haven't seriously used Kotlin in 4-5 years. I too was excited about it supporting native compiles and web assembly but I have no firsthand experience with how far those efforts got.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by eatonphil on Wednesday January 11, 2023 @02:32PM (#34343335)
Swift has the distinction (for better and worse) of not being tied to the JVM.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @02:41PM (#34343468)
Kotlin-native is an effort to compile directly to your target non-jvm machine.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by neonsunset on Wednesday January 11, 2023 @03:33PM (#34344270)
Another scenario where an often missed and underrated choice would be C#.

Nowadays it runs everywhere and supports a variety of deployment targets (relying on local runtime, packaging it together into a single somewhat compact binary, natively compiling the code (NativeAOT, it's good but has some limitations) or mixing and matching all of the above).

It is also one of the best high-level languages to write performance-oriented code with, especially so with .NET 7 which allows writing cross-platform SIMD code in a similar fashion to Rust's 'portable-simd' crate.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by kitsunesoba on Wednesday January 11, 2023 @03:47PM (#34344500)
My experience is probably colored by .NET/WinUI but when I was dabbling in writing a Windows desktop app a few months ago I can't say I loved C#. My background has no Java in it so maybe that's why, but a lot of things that come standard as part of Swift seem to be in external libraries with .NET/C#, and it felt like some things were different for the sake of being different. Point in case, with Swift and Kotlin I've gotten used to chaining various transformation functions (map, compactMap, flatMap, filter, sort, reduce, etc) and some of these don't have a 1:1 equivalent in C# which was a huge hit to productivity, even if the same result is achievable via other means.

And while it's tangential, Xaml drives me absolutely bonkers. It's like the worst parts of iOS Storyboards and Android Framework XML layouts except there's no escape hatch for those looking to build a UI in pure code (Android Framework is a bit of an offender here too, but Jetpack Compose looks to remedy that).

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by neonsunset on Wednesday January 11, 2023 @04:23PM (#34345067)
All these are part of standard library. Map is Select, Filter is Where, Reduce is Aggregate, etc. You can write C# in a functional style easily, it is one of the premiere features.

UI frameworks on the other hand…I feel your pain.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by kitsunesoba on Wednesday January 11, 2023 @04:31PM (#34345194)
I did find and use Select and Where at least, but I recall running into caveats with them or with their interactions with other parts of the language that meant that they couldn't be used identically. I don't remember specifically what it was since it's been a while but I remember it being frustrating. I should probably find a cheat sheet of equivalents if I try writing it again.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Jensson on Thursday January 12, 2023 @01:11AM (#34349825)
The C# equivalents uses SQL naming, knowing this makes it much easier to understand.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by FlyingSnake on Wednesday January 11, 2023 @04:16PM (#34344979)
> some of these don't have a 1:1 equivalent in C# which was a huge hit to productivity

This is interesting because all these magical functions (zip, map, Rx etc) have roots in LINQ which sprung from .Net world. I find it hard to believe that the battle tested CLR and C# doesn't have the equivalent functions.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by lmm on Wednesday January 11, 2023 @05:44PM (#34346152)
> all these magical functions (zip, map, Rx etc) have roots in LINQ which sprung from .Net world

No they don't. They're essentially unchanged from ML back in the 1970s. The part that was new in C# was the SQL-like syntax on top of them, and most subsequent languages haven't considered that worth adopting.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by kitsunesoba on Wednesday January 11, 2023 @03:50PM (#34344543)
Kotlin is about as nice as Swift until you run into some bit where it's interfacing with Java and then it becomes a lot less fun. Swift I think handles Obj-C/C interior a bit more gracefully by comparison. Various bits of other JVM baggage also aren't great.

That said I've only used Kotlin in the context of Android development. It might be nicer elsewhere.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by FlyingSnake on Wednesday January 11, 2023 @04:12PM (#34344932)
Apart from language syntax similarities, Kotlin and Swift are fundamentally different languages, one runs on a VM (ok there's KNative) and other runs on bare metal.

Kotlin has it's roots in JVM and the early language design choices clearly reflect that. Kotlin/Native will find it very hard to break free of it's JVM counterpart because it can't diverge too much from it to maintain compatibility.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by jurip on Thursday January 12, 2023 @12:41AM (#34349637)
Background: I spend most of my time in Swift but do work in Kotlin too. Occasionally some Rust.

Swift and Kotlin are in many ways similar, but I find Swift to be a bit nicer in almost every respect. Swift has more powerful generics, tuples, structs, powerful enums that are value types, `if let`/`guard let` statements that I'll take over `?.let {}` any day, etc. Kotlin is somewhat more expression oriented, which is nice; Swift is moving in that direction too, but slowly.

A native Kotlin (I have no idea how mature it is) might be a decent alternative to high-level Rust, but I'll take Swift if it's available. It's both more pleasant to work in and I assume more performant with better access to stack allocated value types.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by theLiminator on Wednesday January 11, 2023 @02:19PM (#34343142)
Afaik Swift is pretty close to using Rc and Arc everywhere. Does swift do some more escape analysis than Rust?
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by janef0421 on Wednesday January 11, 2023 @02:54PM (#34343695)
Swift only uses reference counting when working with objects; Structs are optimised using copy-on-write. There's currently work on implementing move and ownership semantics, similar to Rust, but opt-in rather than by default.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by meindnoch on Wednesday January 11, 2023 @06:50PM (#34346841)
Ummm… No.

Swift structs are just like C structs (from a memory perspective). The copy-on-write thing is implemented manually by storing a private refcounted object in your struct. See the implementation of Array for example: https://github.com/apple/swift/blob/main/stdlib/public/core/...

There’s no magical copy-on-write mechanism at the language level.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by jshier on Thursday January 12, 2023 @01:53PM (#34358570)
I don't know why this is downvoted, it's true. There is no automatic copy on write optimization in Swift. It's a manual optimization that expensive types like Array implement manually.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by meindnoch on Thursday January 12, 2023 @03:20PM (#34359739)
>I don't know why this is downvoted, it's true.

I know, right? Some people are pretty insecure…

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by manmal on Wednesday January 11, 2023 @02:58PM (#34343754)
I haven’t read that much on Rust, but that should be pretty much it.

Swift has an emphasis on value types, which often are stored on the stack, but only up to a certain size. Copy-on-write makes this feasible - often, those value types are passed by reference until a write actually occurs, but that’s opaque to the dev. Value types can have pointers to reference-counted types - if a value type is passed/copied, any pointer it owns is retained (weirdly enough, they claim that value types have no „lifetime“, but at some point those refs have to be released - we just have to trust the compiler in this).

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by rattray on Wednesday January 11, 2023 @02:42PM (#34343497)
Interesting. Asking as someone who's never programmed with Swift or Rust, but is familiar with the syntax/ideas of each, I'm curious why you say that?
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @03:17PM (#34344017)
If you don't care about performance and "zero cost abstractions" and are more interested in Rust for its memory safety, then most of your Rust programs end up with a lot of syntactic and library bloat to add those "costly" (but convenient) abstractions back in, like explicitly cloning or wrapping types in Rc and Arc or just always taking ownership of borrowed data by constructing owned types with it. And then there are lifetimes which can be and are elided in almost all uses cases so they're ultimately just confusing whenever you actually have to deal with them. C.f. Swift, where the default is in my experience what "higher level" rust programs end up looking like, but with much nicer and cleaner syntax. And Swift has a very nice rather memory safe API for calling into C or otherwise just generally accessing scoped pointers when needed. This same api can actually be used to expose more performant apis to underlying data etc. when needed. It's kinda the reverse of Rust.

It feels like Rust is two languages in one: a high performance zero cost abstraction language that tracks pointer ownership, and a package rich and hyper explicit application language build atop all the guts. That's why I say high-level application use cases, because most high-level applications are not concerned with raw performance but rather with functionality and user experience. The parts that are concerned with throughput can be implemented using tooling where those knobs are available. I have enjoyed writing a cli and api server and various libraries in Rust. But every so often I am left wondering when Swift might be able to replace it for my higher level concerns. Alternatively, it would be neat to see some effort put behind a "convenient rust" type of compile mode for modules where you could compile with things like implied clones, unified owned vs reference types, auto-arc/box, etc.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by rattray on Wednesday January 11, 2023 @04:32PM (#34345215)
Makes sense, thank you for explaining!

Personally I'd love to see a forked flavor of TypeScript with static compilation and multithreading (which implies a lot more immutability, etc). Maybe I should give Swift a try…

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by yyyk on Wednesday January 11, 2023 @05:48PM (#34346205)
>If you don't care about performance and "zero cost abstractions" and are more interested in Rust for its memory safety...

Just about GC language has similar tradeoffs. The question here would be why Swift over Go/C#/Java/Python/Typescript?

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by rattray on Wednesday January 11, 2023 @07:35PM (#34347350)
Well that's easy. Swift is:

1. Much more expressive and featureful than Go

2. More concise and modern than C# (debatable maybe?)

3. Less JVM than Kotlin (the apples-to-apples; Java is a much worse language)

4. Way, way faster and more typesafe than Python

5. Better support for parallel execution (and probably fine-tuning other performance knobs) than TypeScript

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by yyyk on Thursday January 12, 2023 @07:35AM (#34352575)
1. Definitely, but Go is well very optimized for concurrent network apps.

2. I think the C# team has done very well modernizing. The runtime is a bit of a bother though.

3. There's Kotlin/Native and Kotlin/JS.

4. Python has dev speed and ML benefits.

5. If you're going this way, performance probably isn't your main goal?

All-in-all, Swift is interesting, but (IMHO) lackluster crossplatform support and toolchain is a problem.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Thursday January 12, 2023 @11:27AM (#34356005)
I have used all those languages seriously save C# (only lightly) and can safely say Swift knocks them all out of the park in terms of actually writing applications. Indeed, its cross platform support is lackluster, thats why we’re here.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by zozbot234 on Wednesday January 11, 2023 @06:29PM (#34346611)
> Alternatively, it would be neat to see some effort put behind a "convenient rust" type of compile mode for modules where you could compile with things like implied clones, unified owned vs reference types, auto-arc/box, etc.

If you can come up with a precise transform from "convenient/sloppy rust" to the underlying language, it can already be implemented via proc macros and the #[attributes] syntax. This is how async programming was prototyped in Rust before it became part of the language proper.

Though I'm not sure it's fair to describe Rc, Arc, Cow etc. as "library bloat". It certainly adds some boilerplate, but it's designed to stay manageable.

(Arguably, good coding practice should also informally document why the Rc, etc. is needed and can't be refactored away, i.e. what parts of the program are controlling the lifetime of each Rc'd object.)

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @07:18PM (#34347147)
Maybe bloat wasn't the right term (was thinking that it bloats the code, not the resulting program), but when you're in a scenario where you're doing one thing more often than the default, it feels like boilerplate to have to continually say `String::from("some string ref")` (as an example) just to take ownership when that's what you want every single time. Or, at least something like an "owning assignment" or "owning deref" operator that automatically adds the `to_owned()` call would help reduce boilerplate.

Maybe it was a mistake to conflate lifetimes and generics. It wouldn't be so bad to deal with references in Rust if you didn't have to include lifetime parameters when building APIs. It seems silly that (in my experience) people gravitate toward structs with owned fields just to avoid specifying the lifetime of a borrowed field.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by ChubbyGlasses on Wednesday January 11, 2023 @02:50PM (#34343625)
Swift and Carbon both seem like very strong contenders in this space. Swift is already a really strong language albeit a little too tied to Apple’s ecosystem. This along with the ownership manifesto slated for Swift 6 (and C++ interop) should make it easier to use it everywhere. In particular, I see a lot of value for tooling (eg JS code bundlers and such) that are rewritten in “thinner” languages for performance needs without sacrificing developer productivity.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by pkulak on Wednesday January 11, 2023 @02:51PM (#34343636)
I don't know if you can write high-throughput systems with 100% reference counted GC. Swift is targeted at UI work pretty hard.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by dcow on Wednesday January 11, 2023 @03:22PM (#34344093)
Well that's why I said high level application use cases and not systems. Ideally I'd love to see a compile mode for modules where certain conveniences were enabled like: auto-clone, unified reference/owned types, disabled lifetimes and instead borrows become auto-wrapped-arcs, etc. That way you could care about that stuff in modules where it matters, and ignore it in areas where you prefer convenience over zero-cost abstractions and raw performance.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by georgelyon on Wednesday January 11, 2023 @03:54PM (#34344607)
You could take a look a swift-nio (https://github.com/apple/swift-nio) which is a pretty high-throughput system. swift-nio does this using some reference-counted GC where it simplifies the code and doesn't affect performance. Otherwise, value-types are used which incur no GC overhead (unless they are copy-on-write, and backed by something that requires reference counting).
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by kevinossia on Wednesday January 11, 2023 @10:36PM (#34348869)
Sure you can. C++ has reference-counted garbage collection with its smart pointers and RAII, just like Swift. People certainly write high-throughput systems in C++.

Swift is slower than C++, yes, but not because of its memory management scheme.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by pkulak on Thursday January 12, 2023 @02:27AM (#34350287)
Unless you go absolutely ham with smart pointers (which everyone knows not to do if you care about performance), C++ isn’t really a reference counting language.

I wouldn’t call RAII “reference counting”. I mean, I guess, but it’s the programmer or the compiler doing it. I’m talking about runtime reference counting.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by FlyingSnake on Wednesday January 11, 2023 @04:07PM (#34344859)
If the foundation is rewritten in Swift, it will become a very potent alternative to Rust for this use case. Swift+LSP has good enough IDE support and the developer experience and debugging capabilities should make possible.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by seanalltogether on Wednesday January 11, 2023 @04:58PM (#34345570)
I think the fact that Rust won't provide inheritance based OOP workflows means that something like swift will always be valuable, especially for application development.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Gigachad on Wednesday January 11, 2023 @05:05PM (#34345657)
What is the value provided by inheritance? It was my understanding that this was deliberately excluded from Rust as it's now seen as a bit of an anti pattern mostly inferior to traits.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by sacnoradhq on Wednesday January 11, 2023 @05:19PM (#34345856)
None. It's code organizational syntactic sugar and abstraction that incurs a runtime penalty (vtables).

Go ditched OO for similar reasons.

Haskell, Erlang, and more get along fine without OO.

(OO crested with Smalltalk, Java, Ruby, Python, and JS (more prototypal though). Let's not talk about C++98)

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by metaltyphoon on Thursday January 12, 2023 @11:33AM (#34356137)
Don’t trait objects work the same way as vtables (fat pointer)?
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Gigachad on Thursday January 12, 2023 @08:23PM (#34362787)
Monomorphisation solves this. The compiler duplicates functions for every type you use them with in the finished binary.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by seanalltogether on Wednesday January 11, 2023 @07:10PM (#34347069)
Inheritance is about providing business logic in all downstream inheritors through a super() chain. If you want shared logic in Rust between several implementors of a trait you will probably end up with duplicate code. There are drawbacks to both.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by Gigachad on Wednesday January 11, 2023 @08:20PM (#34347779)
You can solve duplication with macros to implement traits. The Rust way seems to provide the same convenience without the runtime costs.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by seanalltogether on Thursday January 12, 2023 @04:42AM (#34351239)
Macros provide code injection, that's not the same thing. Inheritance allows for overrides so you can have [default super logic]+[custom child logic]. That's only possible in rust with a lot of copy and paste, which can be fragile to maintain long term. Most UI frameworks are built around inheritance to guarantee logic provided by the base view or view group, while allowing for inheritors to mix in custom logic as well
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by lmm on Wednesday January 11, 2023 @05:47PM (#34346196)
> I am pretty certain that most of high level use cases for Rust could be replaced with Swift, with increased developer velocity, if Swift was actually cross platform.

Sure, or really OCaml or any other language with ML-family features (other replies have already mentioned Kotlin or C#). But the fact that it took Rust to get adoption and not OCaml suggests that it's not language functionality that drives adoption; Rust has something that those languages don't. (My theory is that it succeeds by being the first decent language that can match C's performance-on-silly-microbenchmark numbers)