Hacker News new | ask | show | jobs
by losvedir 2460 days ago
On paper, Swift is my perfect language. It's like a slightly higher level rust.

For a while I was enamored of Haskell and its famed "if it compiles it works" saying, but I think I've realized that the features that contribute to that are: non-nullability and algebraic data types. Turns out I don't particularly like the extreme functional nature or (especially) the lazy aspect of it.

Rust is almost there, but then I'm dealing with lifetimes and working a little lower than I usually need. But I found I _love_ its exhaustiveness checking for enum variants.

The ML family is pretty much in my sweet spot, for some reason it doesn't really get the love that it seems it deserves, and last I heard Ocaml had an issue with being single threaded.

So that leaves Swift, with its non-nullability, algebraic data types, and exhaustive enum pattern matching. The only downside is its focus on Macs! I'm holding my breath, waiting for it to run just as smoothly on linux, and developing some developer cultural cachet and libraries for servers. I hope it does! I'm glad to see in this article that it's being used more and more at Apple.

And with SwiftUI, which looks pretty promising, I have to imagine it will only continue to grow in popularity.

13 comments

Swift feels like a Frankenstein language though, there are so many keywords and new ones get added in every update. This article here [1] stack ranked a few languages by number of keywords and Swift is #4 on the list. And in Swift 2.1 they added some more, like properties wrappers and the `some` keyword. It gets hard to keep track of them all and the special behavior each one implies.

On the other hand, you can kind of make your own language within Swift because most of the keywords aren't required but allow some additive features. For example, some features help you write your code functionally and others support a declaritive style. It reminds me of what's going on with Babel and JavaScript, where babel extensions are like these language plugins you can mix and match to create your own language.

If you think of metaprogramming as a way to build your own language within a language, lisps and Ruby were kind of like pioneers of a build-your-own-language trend. I could see that as an interesting direction, I wonder if people have tried it before.

[1] https://medium.com/the-traveled-ios-developers-guide/swift-k...

The thing that makes this work in Swift is the concept of Progressive Disclosure. One of the main tenants of the language is that it should be possible to be productive with a small, high-level subset of the language, and then you can gradually start to utilize the more powerful and obscure parts of the language as you find needs for them

> you can kind of make your own language within Swift

This is one of my favorite aspects of working with Swift. Because it's a relatively un-opinionated language with a diverse toolkit, and because of the powerful type/protocol system, it can really feel like you can carve out a DSL for each specific use-case.

> The thing that makes this work in Swift is the concept of Progressive Disclosure. One of the main tenants of the language is that it should be possible to be productive with a small, high-level subset of the language

"Progressive Disclosure" is an interesting idea for PL research, but IME Swift doesn't do a very good job at it. There's a million cases I've seen where people trying to do simple things run into a pandora's box of complexity (e.g., the infamous "Protocol 'P' can only be used as a generic constraint because it has Self or associated type requirements"). There isn't really a "small subset" I've found that's usable for anything but the absolute simplest programs. Even the fundamental types like Int and String have a lot of not-entirely-hidden complexity in Swift.

> Because it's a relatively un-opinionated language

It seems awfully opinionated to me. There is a clear preferred way to write almost anything. Ask online about how to do things differently, and you'll be told to do it in a more "swifty" way.

> because of the powerful type/protocol system, it can really feel like you can carve out a DSL for each specific use-case

I'll have to see if the new features in the latest versions of Swift (used for SwiftUI) enable this more readily, but as of 4.2, anyway, writing DSLs in Swift is moderately limited and painful.

> Swift feels like a Frankenstein language though, there are so many keywords

That's a big reason why I don't like it. I also can't stand func, let, and var. Because func and var are totally useless, and let is the wrong word (should be const at least).

There is also all the question marks and other confusing syntax.

let is the right word given the historical background of functional programming languages.
nit, only mentioning it because I'm super curious about that list: searching for "4" and "rank" in the article doesn't show any relevant results to the ranking
ah sorry I pasted the wrong link, here it is: https://medium.com/@richardeng/how-to-measure-programming-la...
More accurately, it's tied for third place with C++.
That doesn’t mention PL/I, so it’s the competition for amateurs. https://www.cs.vu.nl/grammarware/browsable/os-pli-v2r3/:

    Summary
    Number of keywords: 310
PL/I made life ‘easier’ by not making keywords reserved, allowing you to use variable names that match the strings used to identify keywords, though, so you didn’t have to learn them all.
They’re certainly wrong about Scheme. They used MIT-Scheme to count keywords, when they should’ve used R5RS Scheme or newer. R5RS has 20 keywords + 4 if you add support for macros. With define-syntax you can define many special forms such as let, let* etc in a smaller language so may be the actual number is much smaller!
They don't say what version of Swift they're counting, but I count 96 keywords on the documentation page they link to, which would move it above C++ into 3rd place.
oh wow, smalltalk has only 6 keywords. that's really... small :).
> Rust is almost there, but then I'm dealing with lifetimes and working a little lower than I usually need. But I found I _love_ its exhaustiveness checking for enum variants.

Out of curiosity, when's the last time you worked with Rust? A lot of the lifetime's have been able to be elided in recent releases, especially in the 2018 edition. That's not to say that you don't still run into them, but in the middle of the road cases, it's much less frequent that you run into them unexpectedly.

Having written a lottt of both Rust and Swift there is something about Swift that makes it "flow" far easier than Rust.

I haven't been able to narrow down exactly what it is, but with Swift I can just put pencil to paper. With Rust I feel like I need to plan more, I dunno.

I do think my experience is colored by tooling, though

I briefly built a Swift app. I hated Xcode, but Swift was by far my favorite language I have ever worked with.

I'd love to see a Rails like framework come to Swift. I don't think we'll ever see that.

If you like the Swift language, you would also like Scala, and Scala has Play, which is Rails like. Also, in anticipation of that question, yes, Scala can be AOT compiled using Graal.

https://leverich.github.io/swiftislikescala/

There is one at least! www.vapor.codes, it’s really great.
And that unlike Rust Swift is often much slower if you aren't extremely careful how you use it
Swift has a different niche than Rust. They both offer safety, but Rust prioritizes performance, where Swift prioritized ergonomics.

For me personally, Swift is often fast enough for my needs as a compiled language, but it's so much easier to be productive in since it obviates many of the more tedious concerns of Rust.

Both languages play nicely with the C FFI so it's also possible to write high-level code in Swift and drop down into C or Rust for the performance sensitive parts.

There is also F#, open source and available in .net core on multiple platforms
I love F#, and the whole .net environment. It's very performant, (and C# isn't a bad language either for you OO fans). GC is ok by me.
Funny, the reason why I ended up not exploring Swift further, and instead trying out F#, was due to Swift's lack of tracing GC.
> I'm holding my breath, waiting for it to run just as smoothly on linux, and developing some developer cultural cachet and libraries for servers.

Apple Swift developers are actively working Linux support, but I suppose it'll forever remain a second-tier platform to iOS & macOS. Kind of like C# to Windows, but Swift doesn't have its Miguel de Icaza to create its Mono on Linux.

I'm sure the Swift team is well aware of this cultural bottleneck, hence the current efforts to support Linux, but it remains a cultural chicken-and-egg problem.

It's not just Apple working on Swift on Linux. IBM for example has done a ton of work. A Facebook engineer has contributed a HUGE amount of work to bring it up to speed on Windows (without WSL or Cygwin). And Google will end up contributing on all fronts if they continue to push it in the ML space.
> last I heard Ocaml had an issue with being single threaded.

OCaml is single-threaded, yes, but in practice that's not a big deal. There's a great concurrency library (Lwt) and the recently-added monadic (and applicative) bind syntax makes it look almost like sequential code. And if you really need a separate OS thread, there are ways to do that :-)

Last I checked, Swift still does concurrency mostly with Node-style callbacks.

Swift is my dream programming language as well, but I tried (2 years ago) reimplementing some python scripts in it and I had a hard time with things as simple as printing to the terminal (which involved distinct libraries and methods for mac and linux), and other things I don't remember very well at the moment.

But I do remember having to implement myself protocols for standard library types, most of them obviously belonged to the standard libraries, they even had theses protocols as examples in Apple's docs.

The language is truly great, but was not ready for primetime out of Cocoa apps.

Has anything changed?

Swift does run on Linux and Windows. Getting to usable is a function of community; waiting for one means you won't be a part of helping build it.
Did you tried scala? A quite mainstream functionnal programming language inspired by the ML family. I personally love it.
Scala is too big and complex. I tried to learn it, but ended up using it as a better Java with some functional features like pattern matching. I felt like it wasn't worth to continue mastering Scala after a certain point.
The funny thing is, those are not expensive or difficult things to implement in a compiler.
How about Scala?
It’s a really nice language. I like it a lot. But it has a dependency on a pretty hefty runtime. I understand that packaging nowadays is not really a problem, but still.
You should check out GraalVM. Easy compilation to native code. A total game changer for the JVM, imo.