293 points · by goranmoomin · 2y ago · 153 comments
Ex:
enum MyEnum<T> {
case one
case two(twosChild: Child)
case three(threesGenericChild: T)
case four(foursCallbackChild: () -> Void)
}You can then pass this enum around and switch on it, with compiler-guaranteed completeness everywhere in your codebase. It seems so freaking simple, but it's incredibly useful.
F#, OCaml, and Standard ML are the primary examples. It's surprising to me how ignored these languages are.
> it has so many simple features I love that I am sometimes surprised to find are missing in other languages
and a few other things that led me to believe they were unaware of discriminated unions. There was an implication that this was a unique or novel feature in Swift. It's like someone being excited about Python's pattern matching, thinking Python came up with it and wishing other languages had it when it's a watered down, less useful version of that found in languages it ignored for 30 years.
The day C# gets discriminated union types and true exhaustive pattern matching is the day I’ll have to think long and hard about how much more time I want to put into working with my beloved F#.
Example why it would be sweet. Compiler could spit out a warning if it's not sure the correct union type is being used. Like if the pet union has dog and cat. Cat has respawn set initially to 9. Dog doesn't have respawn. If you try to set respawn without checking if it's a cat compiler whines about it.
Funnily enough, I sort of had the opposite journey, Swift seems horribly complicated and over-specified. The Dart compiler knows if I do a null-check, it can treat following references as safe, instead of if let x = x etc.
And it just hasn't lived up to any of the initial starry-eyed hype I had for it and expectations for a wider Swift ecosystem...
Like, here, it's great it runs on Raspberry Pi Pico but...it can't render UI unless someone writes a new UI library from scratch. And whoever does that inevitably will have to copy SwiftUI directly or alienate the people who actually know Swift.
I may be wrong, but I don’t think this is actually true. SwiftUI isn’t universally loved in the community, largely thanks to its rough edges and how it struggles with that last 10% of polish in projects written with it. While copying SwiftUI wouldn’t necessarily be wrong per se, you’d actually probably get more rapid buy-in by copying “boring” old imperative UIKit, as that’s what most writers of Swift are using.
Point is its DOA for UI on non-Apple platforms.
Either you:
- attract current Swift programmers and keep up with $APPLE_UI_FRAMEWORK with no assistance.*
- attract no one and make a brand new framework in Swift.
- bind to some other UI framework, alienate current Swift programmers*, get all the people who have been dying to write Swift yet didn't build a UI framework for 7 years
There's just no universe where using Swift to write UI on non-Apple platforms makes sense to any constituency. You have to be simultaneously obsessed with Swift and don't care about platform-first UI, or want to maintain two separate UI frontends.
I hate being that absolutist, but that is what 16 years of experience on Apple platforms has taught me.
I'm surprised by the # of people arguing there's a possibility something else coudl work. I guess what I'd say is, if there is, where is it? Swift was on Linux eons ago, I believe before I started at Google so 7+ years.
* People used to try this to do iOS/OSX cross-platform, those projects are dead. One could argue this is just because Catalyst exists. Sure, but, let's just say you need at least N / 2 engineers, where N = the number of engineers Apple has on an arbitrary UI framework, in order to keep up with it. That's a lot.
** I've been in Apple dev since iPhone OS 2.0, and while in theory this could work, in practice it doesn't. Ex. Qt won't appeal to Apple programmers. It's antithetical to community values.
`if let x = x` is an alternative to a null check, it's not as if now you need both a null check and an `if let`. What makes an `if let` more complicated?
`if let x { }`
Which while small, is great if you do it all the time.
Swift though made me question how much I used optionals, now I get annoyed if they're over used, it usually means someone down the line has not made a decision, they propagate through the program and eventually someone wonders why they didn't get a value.
I like the Result type and the Flexibility of the Enums a lot for this
This is true for literally every programming language. You can’t render a UI, in general, without a target compatible UI library.
SwiftUI and UIKit are specific Apple things built on top of CoreAnimation and completely separate from the Swift language. And swift will happily link to C and somewhat C++, so no it’s not all evident why one would copy Apple’s SDK to render UI on a completely separate platform and environment.
Does Dart bind a new variable 'x' for the if-statement scope or does the if-statement scope refer to the same 'x' from the outer scope?
The reason Swift and other languages bind a new 'x' variable (if let x = x) is because the original 'x' can be assigned a different value elsewhere, like in another thread or a closure. If a closure captures and assigns null to 'x' and there is only one binding of 'x', then that means the 'x' within the if-statement will become null _post null-check_ which is type unsound. I believe TypeScript has this "problem" as well.
1) Dart doesn't have threads, it has isolates (where memory is not shared). So no variable modifications here.
2) Dart will promote a variable as not null after a non-null check only if it cannot be modified elsewhere, ie, the variable is final (can't be modified once assigned), or there's no other reference to it.
To clarify, in the above scenario in dart I can have an input with type MyEnum and do this?
switch myEnum {
case let one:
// react to this here
case let two(twosChild: twosChild):
// call an API passing in a child element of twosChild here
case let three(threesGenericChild: threesGenericChild):
// T: Protocol, and call an extension function threesGenericChild.extension() here?
case let four(foursCallbackChild: foursCallbackChild):
// Pass foursCallbackChild callback into another context, and execute an escaping closure if needed?
}Last I checked Dart required a nested if/else tree and didn't have compiler-guarantees of switch completeness like the above.
The syntax is unfortunately more verbose, but if all goes well we will address that issue this year.
sealed class MySomething<T> {
}
final class One extends MySomething<Never> {
}
final class Two extends MySomething<Never> {
final Child child;
Two(this.child);
}
final class Three<T> extends MySomething<T> {
final T value;
Three(this.value);
}
final class Four extends MySomething<Never> {
final int Function() callback;
Four(this.callback);
}
And then you can exhaustively switch over values of MySomething: int foo(MySomething<String> foo) {
return switch (foo) {
One() => 1,
Two(child: Child(:final value)) => value,
Three(:final value) => int.parse(value),
Four(:final callback) => callback(),
};
}
The declaration of a sealed family is considerably more verbose than Swift - and I really would like[1] us to optimized things for the case when people often declare such families. Macros and a primary constructors will likely provide reprieve from this verbosity.But importantly it composes well with Dart's OOPy nature to achieve things which are not possible in Swift: you can mix and match pattern matching and classical virtual dispatch as you see fit. This, for example, means you can attach different virtual behavior to every member of the sealed class family.
There’s actually several bits of Kotlin like this which feel somewhat pedantic/idiosyncratic and fussy if you’re used to writing Swift. I’m sure there’s valid design reasons behind these decisions, but sometimes it feels like unnecessary friction.
I feel ML is the most famous for it.
Another place Swift seem to be getting traction in is godot.
Miguel De Icaza’s talk on the topic is highly recommended:
https://m.youtube.com/watch?si=WNxwcTPGFbJABKez&v=tzt36EGKEZ...
I wish it gains more popularity outside Apple land and is discussed more like Zig or Rust.
If I could use a minimized Swift on embedded, I'd be very happy. Right now I'm just doing C for ESP32 and that sort of thing.
What if Apple stops development on other platforms, or makes future development proprietary who is going to maintain it for other platforms?
On the flip side, Swift provides a lot of syntactic sugar that makes it feel faster/smoother to engage with than Rust. For example, enum literals don't have to specify the enum's name wherever their type can be inferred by the compiler. So e.g. when you are pattern matching on an enum value, the match arm `MyEnum::Red => {}` in Rust would just be `case .Red:` in Swift. It isn't much on its own, but each tiny QoL thing like this adds up.
I find Rust a bit easier for me to read, quite preference-driven as I prefer snake_case to camelCase but that's a tiny thing.
And weirdly enough I think I actually prefer `MyEnum::Red => {}`, but maybe that's just stockholm syndrome.
Excited to give Swift a try sometime soon.
> No macros / build-time code generation
While I do appreciate macros, and use them sparingly I find the best case for them is doing things like generating to/from <protocol> (e.g. JSON) implementations.
In Haskell, there's a fully baked system for derivation where you can set up implementations that walk the type trees, but most other languages don't have that -- I find macros to be a great slightly lower complexity way to do this kind of thing.
What is Swift's answer in production? Does everyone just write extensions for their types? Rust's serde is a joy to use and 99% of the time "just works", despite the macro magic.
> Type system function resolution
I'm not sure this is a real problem for me or most people, but taken with the point about polymorphism (I guess this point is basically about late binding), I see the benefit here.
> Protocols vs Traits as implemented
Generic methods being allowed in Swift is quite nice.
> Sugar
Great points in there -- some of them are style/preference but I can definitely see benefits to a bunch of them.
The string interpolation point you might want to update (Rust does that now!)
Named arguments are also quite amazing.
Again, fantastic list -- saved! I've been meaning to give Swift a proper try lately and this is a great reference.
Can't complain though my current daily driver is Scala and that's also a fantastic language to work with.
As for Swift libraries, Stdlib is fully ported, Foundation can be spotty, the new Foundation-Essentials is small but growing. Swift-System has portable low-level path and such, and Shwift helps with fully async scripting.
As for supported platforms, Ubuntu seems robust, AWS is usable but marginal. Windows has some hurdles - technical, legal, and incentives - but VSCode support seems relatively good for non-GUI stuff outside windows API's.
Embedded has been landing in the main compiler and pushed by the Swift team lead. They're still issue-spotting, planning how (much) to live without a heap and the runtime metadata used for protocols, but the number of issues seems manageable for the intensity of effort, even though it's not Apple's priority.
With Swift 6.0, a breaking release, up next (in September?), it's likely embedded features could be both driven by the desire to make any breaking changes now, and blocked by other priorities.
One addition: Swift WASM support has recently become stable enough to be [included in CI](https://forums.swift.org/t/stdlib-and-runtime-tests-for-wasm...). The folks working on that have done a great job and I’m very excited for that to get more fleshed out!
IBM Stops Work on Swift — Q&A with Chris Bailey :: https://www.infoq.com/news/2020/01/ibm-stop-work-swift-serve... ( 43 points | 3 comments https://news.ycombinator.com/item?id=22175652 )
In this (and highlighted in a comment) there's a quote:
> Swift benefits greatly from being an embedded part of Apple's ecosystem, but it also provides challenges for its organic growth as there's a lot of dependency on Apple for capabilities that the server ecosystem requires. For example, almost all Swift developers use Apple's Xcode as their IDE and it has fantastic support for developing for iOS devices, including the ability to run locally in emulator environments. It would be great to add support for writing server code that allows developers to develop directly into containerized environments from their local IDE by supporting the simple integration into Xcode of development tools such as Appsody. Where there is open governance and open ecosystems, it enables the community to contribute and to solve the problems and use cases that are important to them.
Same timeframe: IBM Stops Server Side Swift Framework Development - https://news.ycombinator.com/item?id=21808619 (156 points | 97 comments)
---
From earlier times:
Introducing the IBM Swift Sandbox - https://news.ycombinator.com/item?id=10676655 (246 points | 59 comments)
IBM says Swift is ready for the enterprise - https://news.ycombinator.com/item?id=12581250 (176 points| 226 comments)
The language server seems a bit clunky on Windows, I’ve only used Swift with Xcode on macOS so I don’t know if that’s platform specific.
This means it probably has the most Windows Swift code of any app out there and that’s why I mentioned it.
Linux support is i think a bit better. But for now it's still the biggest handicap for the language for sure. Any sane project would have solved those issues a long time ago. But since it's an apple language, they seem to not be caring much.
I usually test them on a Linux VM and a Raspberry Pi
I’ve spent countless hours working in it and crashes and errors have been rare for years now. Especially since ditching CocoaPods in favor of Swift Package Manager for dependencies, I’ve found that Android development to be considerably more frustrating with the mess of Gradle and Proguard paired with the surprisingly anemic Android Framework. Jetpack Compose is thankfully replacing Android Framework and is a major improvement, but from time to time Gradle and Proguard make me want to tear my hair out.
I tried the IntelliJ IDE, which didn't feel so shoddy, but in turn has a lot of other problems that boil down to not being the "blessed" solution.
It’s pretty baffling how bad Xcode is. Although at this point I’m not sure to what extent Swift is to blame.
Everything seems to work much better when Swift is abandoned and I just use UIKit.
(And it’s a personal blessing when I get to use Flutter and bypass 90% of the insanity that is modern native iOS and Android dev)