this post was submitted on 27 Aug 2023
39 points (88.2% liked)

Programming

17024 readers
256 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

I’ve been programming for decades, though usually for myself, not as a profession. My current go-to language is Python, but I’m thinking of learning either Swift (I’m currently on the Apple ecosystem), or Rust. Which one do you think will be the best in terms of machine learning support in a couple of years and how easy is it to build MacOS/ iOS apps on Rust?

all 42 comments
sorted by: hot top controversial new old
[–] aggelalex@lemmy.world 21 points 1 year ago* (last edited 1 year ago) (2 children)

Swift has little to no use outside the apple ecosystem, and even if you are currently using Apple, you have to consider your targets as well. Writing in Swift means your code will only be usable by other Apple users, which is canonically a rather small fraction of technology users. Rust on the other hand is multiplatform and super low level, there's very few other languages out there that can match the potential of applications of rust code. Thus you will, in time, be introduced to many other technologies as well, like AI/ML, low level programming, web, integrations between languages, IoT, those are only a few of all the possibilities. On the other hand, even if Swift has a much more mature ecosystem, it's still only good for creating UIs in all things Apple, which is pretty telling; Apple is not willing to put in the time and effort to open it's language to other fields, because it sees no value in them being the ones providing the tooling for other purposes. They pretty much only want people to code web apps for them, and Swift delivers just fine for that. So if your current purpose is making Apple UIs, you could learn Swift, but be warned that either you'll either be doing that your whole life or will eventually be forced to change languages again.

Then again, most languages nowadays aren't that different from each other. I can code in a truckload of languages, not because I actually spent time making something coherent and complete with each one of them, but because I know some underlying concepts that all programming languages follow, like OOP, or functional programming, and whatever those entail. If you learn those you will not be afraid to switch languages on a whim, because you'll know you can get familiar with any of them within a day.

[–] farcaller@fstab.sh 11 points 1 year ago (1 children)

Just a nit: swift is opensource and there is a swift ecosystem outside of apple UI things. Here's a swift http server that you can totally run on linux.

[–] aggelalex@lemmy.world 9 points 1 year ago (1 children)

Don't get me wrong, Swift is OSS and there are things you can do with it apart from front-end dev, but there are usually better options out there for those other things. For example if I want an HTTP server, I'd choose JS, Kotlin, Rust, etc.

[–] abhibeckert@lemmy.world -4 points 1 year ago* (last edited 1 year ago) (1 children)

For example if I want an HTTP server, I’d choose JS, Kotlin, Rust, etc.

I wouldn't. Swift is definitely better than any of those choices... and I say that as someone with decades of experience writing HTTP services.

I don't currently use Swift for any of my HTTP servers - but only because it's a relatively immature for that task and I'm generally a late adopter (also, I work in an industry where bugs are painfully expensive). But I do use Swift client side, and I definitely intend to switch over to Swift for my server side work at some point in the near future and it's what I recommend for someone starting out today.

By far - my favourite feature in Swift is the memory manager. It uses an "Automatic Reference Counter" which is essentially old school C or Assembly style memory management... except the compiler writes all of the memory management code for you. This often results in your code using significantly less RAM and better sustained performance than other languages and it's also just plain easier to work with - as an experienced developer I can look at Swift and know what it's going to do at a low level with the memory. In modern garbage collected languages, even though I have plenty of experience with those, I don't really know what it's doing under the hood and often I'm surprised by how much memory it uses. On server side code, memory is expensive and traffic can burst to levels drastically higher than your typical baseload activity levels, using less memory and using predictable amounts of memory is really really nice.

At one point, years ago, Apple had a compiler flag to use Garbage Collection or Automatic Reference Counting. The Garbage Collector worked just as well as in any other language... but there was no situation, ever, where it worked better than ARC so Apple killed their GC implementation. ARC is awesome and I don't understand why it's uniquely an Apple thing. Now that Swift is open source, it's available everywhere. Yay.

I find compared to every other language I've ever used, with Swift I tend to catch mistakes while writing the code instead of while testing the code, because the language has been carefully designed to ensure as many common mistakes are compile time errors or at least warnings which require an extra step (often just a single operator) to tell the compiler that, yes, you did intend to write it like that.

That feature is especially beneficial to an inexperienced developer like OP.

The other thing I love about swift is how flexible it is. For example, compare these two blocks of code - they basically do the same thing and they are both Swift:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create text field
        let textField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        textField.placeholder = "Enter text"
        textField.borderStyle = .roundedRect
        view.addSubview(textField)

        // Create button
        let button = UIButton(frame: CGRect(x: 20, y: 200, width: 300, height: 50))
        button.setTitle("Tap Me", for: .normal)
        button.backgroundColor = .blue
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        view.addSubview(button)
    }
}
struct ContentView: View {
    @State private var text = ""
    
    var body: some View {
        VStack(spacing: 20) {
            // Text Field
            TextField("Enter text", text: $text)
                .padding()
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
            // Button
            Button("Tap Me") {
                print("Button was tapped!")
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .padding()
    }
}
[–] BatmanAoD@programming.dev 2 points 1 year ago (1 children)

I'm not a performance expert by any means, but...it seems like the bit about there being "no situation, ever" in which a garbage collector that "worked just as well as in any other language" outperformed reference-counting GC. The things I've read about garbage collection generally indicate that a well-tuned garbage collector can be fast but nondeterministic, whereas reference-counting is deterministic but generally not faster on average. If Apple never invested significant resources in its GC, is it possible it just never performed as well as D's, Java's, or Go's?

[–] abhibeckert@lemmy.world 0 points 1 year ago* (last edited 1 year ago) (1 children)

Check out this interview with Chris Lattner — one of the world's best compiler engineers and the founder of not only the Swift language but also LLVM which backs many other languages (including Rust). It's a high level and easily understood discussion (you don't need to be a language expert) but it also goes into quite a few technical details.

https://atp.fm/205-chris-lattner-interview-transcript#gc

Chris briefly talks about the problems in the Apple GC implementation, but quickly moves onto comparing ARC to the best GC implementations in other languages. The fact is they could have easily fixed the flaws in their GC implementation but there just wasn't any reason to. ARC is clearly better.

Apple's GC and ARC implementations were both implemented at about the same time, and when ARC was immature there were situations where GC worked better. But as ARC matured those advantages vanished.

Note: that interview is six years old now - when Swift was a brand new language. They've don a ton of work on ARC since then and made it even better than it was, while GC was already mature and about as good as it's ever going to et at the time. The reality is garbage collection just doesn't work well for a lot of situations, which is why low level languages (like Rust) don't have a "proper" garbage collector. Arc doesn't have those limitations. The worst possible scenario is every now and then you need to give the compiler a hints to tell it to do something other than the default - but even that is rare.

[–] BatmanAoD@programming.dev 2 points 1 year ago

Thanks for sharing the interview with Lattner; that was quite interesting.

I agree with everything he said. However, I think you're either misinterpreting or glossing over the actual performance question. Lattner said:

The performance side of things I think is still up in the air because ARC certainly does introduce overhead. Some of that’s unavoidable, at least without lots of annotations in your code, but also I think that ARC is not done yet. A ton of energy’s been poured into research for garbage collection... That work really hasn’t been done for ARC yet, so really, I think there’s still a a big future ahead.

That's optimistic, but certainly not the same as saying there are no scenarios in which GC has performance wins.

[–] mrkite@programming.dev 19 points 1 year ago (2 children)

Rust is the only language I know of that is actively being used at the kernel level all the way through to the web app level. Compare that with Swift which is not only mostly tied to a single ecosystem, but even the "cross platform" stuff like libdispatch is littered with code like:

if #available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)

[–] abhibeckert@lemmy.world 3 points 1 year ago* (last edited 1 year ago)

Note libdispatch runs on older versions of Apple Platforms than those version numbers. The backwards compatible code paths aren't just for other operating systems - that's how it works on older Apple platforms too.

[–] dartos@reddthat.com 9 points 1 year ago (1 children)

I think rust is good for learning some low level concepts, especially coming from python.

I don’t think Python is going anywhere in the ML space though.

[–] Bluetreefrog@lemmy.world 1 points 1 year ago (1 children)

Agree. I'm kinda looking for marketable skills though and I feel Python may be becoming saturated.

[–] dartos@reddthat.com 4 points 1 year ago (1 children)

A programming language itself isn’t a marketable skill!

Learn the underlying concepts of programming and how computers work and you’ll be able to move from language/framework to pretty much any language/framework easily.

[–] Von_Broheim@programming.dev 2 points 1 year ago

Language absolutely is a marketable skill because most companies are looking to hire someone who can start working day one not someone they'll have to train for weeks or even months in a new language that heavily relies on some specific framework.

[–] leclownfou@sh.itjust.works 8 points 1 year ago (1 children)

If you're trying to prepare for a couple of years in advance, it might be worth spending a day playing with each language just to see which one feels best to you. Both languages should be able to do anything you want but some things will probably be more difficult in one or the other. I've never used swift, but I know rust can have a rather steep learning curve. That may be deterrent enough for some people, but that's up to you to decide if that struggle is worth it.

[–] Bluetreefrog@lemmy.world 3 points 1 year ago (1 children)

Thanks, this makes some sense. I've started a few tutorials for Swift, and I added the Rust plugin/module to Visual Studio Code, but neither felt intuitive to me.

[–] leclownfou@sh.itjust.works 1 points 1 year ago

That doesn't surprise me too much. They're both a good bit different than python. It's okay to take a little more time with each of them. Maybe try building one simple thing in both for more of a 1-1 comparison.

[–] 257m@lemmy.ml 7 points 1 year ago (2 children)

Other than having first class support on Apple's hardware Swift dosen't have much going for it. There is no killer feature in Swift, it dosen't widespread features and it only has a small niche. If you want to develop for mainly Apple devices I would say go for it as that is the niche it was designed for. Although I see from your post you want to do ML, Python for the high level stuff + C++ for the low level stuff is probably your best pick for that. May I ask what type of ML are you going for? Are you mainly using libraries like Tensorflow, Pytorch etc... or are you into the nitty gritty of building these things yourself and writing the required code for the matrix math and training algorithms.

[–] Bluetreefrog@lemmy.world 2 points 1 year ago (2 children)

I think ML is probably going to require a lot of people in the future and I'm looking to build a digital nomad skill set for the future that pays well. While I've done a postgrad subject on ML and have a STEM degree, but I'm inclined to use existing libraries as that's just easier.

[–] 257m@lemmy.ml 2 points 1 year ago* (last edited 1 year ago) (1 children)

If you want to train your neural nets you can maybe check out: https://github.com/rust-ml/linfa https://github.com/param087/swiftML (Rust seems to have more active support in terms of libraries)

If you want to integrate ML into an IOS/MacOs app: https://developer.apple.com/documentation/coreml

For userland apps Swift would be better and for training or just being generally being more useful in the future go for Rust.

At the end of the day just choose the language that is more enjoyable for you.

[–] Bluetreefrog@lemmy.world 1 points 1 year ago

Sensational answer! Thank you.

[–] exocortex@discuss.tchncs.de 1 points 1 year ago

There's a recent Rust ML framework called "burn". So maybe there's also a future for ML in Rust for you.

[–] philm@programming.dev 1 points 1 year ago

Swift is a nice language though.

But I'm obviously on team Rust^^ for various reasons (one being that you can do the whole stack in Rust (not that it's necessarily the best choice for each level, but it really composes well and with a little bit of trait-magic abstraction in the higher levels it works quite well IME)

For ML, python yes, certainly for high-level stuff at least currently. I wouldn't be so sure in the future about the lower stack though, Rust seems to gain momentum there as well (potentially replacing use-cases where currently python is dominant too).

[–] farcaller@fstab.sh 6 points 1 year ago

I think Python is still unmatched when it comes to ML, and nothing can beat Swift in terms of Apple ecosystem support. Why not learn both, though? I find Swift a bit harder to reason with than rust, but both have merit (and both have interesting use cases). Just see what uses you will find for them as you progress.

[–] nyakojiru@lemmy.dbzer0.com 4 points 1 year ago (1 children)
[–] Bluetreefrog@lemmy.world 3 points 1 year ago

Lol, Turbo Pascal was the first OO language I learned, back before there was any such thing as an Internet.. Showing my age now.

[–] ericjmorey@programming.dev 3 points 1 year ago (1 children)
[–] Bluetreefrog@lemmy.world 1 points 1 year ago (1 children)

I have thought about Julia.

[–] ericjmorey@programming.dev 1 points 1 year ago

What are your thoughts on it?

[–] rouxdoo@kbin.social 3 points 1 year ago (1 children)

@Bluetreefrog
I, like you, code for myself not others and not professionally. Take a dive into Xcode and Swift if you're in the Apple world. It is just stupid easy to throw together an app or tool in no time at all.

[–] Bluetreefrog@lemmy.world 1 points 1 year ago

Have you played with the Swift ML frameworks at all?

[–] Solemarc@lemmy.world 3 points 1 year ago

If you don't have a Mac I don't think you can get the MacOS SDK.

So in that case I'd recommend Rust. I still think most of Rust's tools/frameworks need more time in the oven but Rust is massive and has tools being built for everything. If you want Mobile I'd recommend you take a look at Dioxus or Tauri. There are probably others as well but I don't know them it's been a while since I've looked.

[–] seeaya@lemmy.world 1 points 1 year ago (1 children)

Something to consider as well is learning both. Swift is certainly the best choice for making macOS/iOS GUIs. Other languages are probably better than Swift for your ML needs (could be rust, Python, etc.). However it’s totally possible to have an app using multiple languages. You could have the UI portion be in Swift, but the ML portions be in another language.

At my company we have a Mac app with the GUI written in Swift, shared logic with our Windows app written in C++, and some libraries written in Rust. So it’s certainly possible.

One caveat is that some languages don’t work with each other very well. Swift and Python do work well together iirc, so doing UI code in Swift and ML code in Python may not be a bad idea.

If you want to just stick to Swift, Apple does have some ML frameworks for Swift that you can use. I don’t do any work with ML, so I have no idea if these frameworks are any good, or have good resources for learning.

If you want to just stick with whatever language you use for ML, there are GUI libraries in nearly every language. These certainly won’t be as robust or as nice to work with as the native frameworks in Swift, but they could probably get the job done. I do know that a major issue with GUIs in Python is the difficulty in multi threading, which is a must for any app that performs long tasks without the UI freezing.

[–] philm@programming.dev 1 points 1 year ago

Just learn whatever you currently need. If you know a few paradigms, learning a new language of the same paradigm is easy-peasy and can be done rather quickly (well at least being be productive with it, doing stuff idiomatically often takes a little bit longer).

That said, Rust IMO is a language that makes sense to learn anyway, since it also teaches you to program in a nicer way (not just true for Rust, there are other languages that have this effect as well, such as Haskell etc. generally languages that introduce something really new (i.e. a new paradigm)). Generally it makes sense to learn multiple languages, as each brings you new ideas. But on the other hand it makes sense to learn one language really well (I'd recommend that being Rust, as it can cover so many use-cases and is generally designed nicely (it fills a sweet spot between mutability and functional programming IMHO).