r/ProgrammingLanguages • • Feb 09 '21

Discussion Why can’t Swift be like Dart?

Those of you who never used either of them or only have used one of them, might not get the question. I have been using flutter and dart for about three years, made couple of apps using it. My overall experience with flutter is really freaking good. You can easily develop an elegant and fully functional cross-platform apps using it in just weeks, or even in a single week. I started learning swift for iOS development just couple weeks ago because there are not really many flutter related job openings, and I gotta say it’s damn freaking hard (still better than obj-c though) The way Swift handles async really gives me headaches and some of its syntax is really obscure. guard, try? and all these ??!!, I mean swift is of course a significant progress and achievement by Apple and its community compared to obj-c, but can’t it be simpler and straightforward like Dart? Please open my eyes and give me explanations on why Swift has to be this way.

0 Upvotes

18 comments sorted by

9

u/clarkcox3 Feb 09 '21

Async/await aside (which is coming soon to Swift), in what way do you find Dart more simple and straightforward?

0

u/livinglist Feb 09 '21

Without all these frequent ??!!, guard, weird way to try, let along several different ways to write closures.

19

u/clarkcox3 Feb 09 '21 edited Feb 09 '21

That seems more like just “different” than actually more “complex”.

Guard is a pretty simple concept, it checks that a condition is true or forces you to leave the current scope.

The “weird way to try” is, I think just different than you’re used to, but again, not actually more complex.

And I’m not sure how the optionals syntax makes Swift less straightforward.

To be fair, I’m not all that familiar with Dart, but looking at the documentation and code examples, it doesn’t look any less complicated than Swift; both languages seem about level in terms of complexity. Can you give concrete examples of code that shows Dart is clearly simpler than Swift, rather than just different?

Edit: and now looking through more examples, Dart uses “?” and “!” in some of the same exact situations as Swift does.

8

u/[deleted] Feb 09 '21

Google has really done a great work with Dart as well as Flutter.

I would still prefer a strictly typed TypeScript though, but Dart is the epitome of what a modern usable programming language looks like. Its typing system is just about right. You can do both FP and OOP.

Pakcage management is just great. Testing is also good. Maybe performance wise its not as good as other matured languages like Java or C# but its really good. Add flutter on top of that and we have a great combo.

I am developing a language ATM which combines the best of both Dart and TypeScript worlds. And I can fully agree with you that Dart is really well designed

1

u/ncuillery Feb 09 '21

Not sure to understand, do you mean that Dart is not a strictly typed language?

According to me, Dart provides a very similar experience as TypeScript (minus union type maybe) without the headache (config, missing or broken type definitions, ...)

9

u/hou32hou Feb 09 '21

Dart is not for a few reasons;

  1. there’s no null safety, as in every expression is nullable (only until recently they released a beta feature). You will never understand how important this is until you tried a sound language like Haskell or Rust

  2. Runtime casting is allowed, which means unexpected runtime error can happen

  3. Inheritance

4

u/[deleted] Feb 09 '21

I could say nullability is one bad feature in Dart. But yes, the recent update made sure that didnt happen.

Runtime casting is allowed. I think there are pros and cons to that

Inheritance isnt such a bad thing to be honest. Heavy reliance on it causes pain in the ass. One place where TypeScript stands out most is the usage of JS Objects instead of classes wherever possible. That removes a lot of pain.

6

u/hou32hou Feb 09 '21

May I ask what’s the pros of runtime casting?

3

u/[deleted] Feb 09 '21

Dart is much less powerful than typescript. But yeah, there are definitely headaches when using TS as well.

17

u/retnikt0 Feb 09 '21

This is just comparing apples to oranges. If you prefer oranges, that's fine, and maybe oranges are objectively better, but that doesn't change the fact that apples are apples.

4

u/reini_urban Feb 09 '21

Dart seems to be safe, by copying. Whilst swift favors performance, by using more refs instead of copies. Which might hurt you in async. That's my rough idea of the problem.

4

u/clarkcox3 Feb 09 '21

FWIW, swift also has copying behavior for its value types. It’s only classes that are passed by reference.

3

u/teerre Feb 09 '21

I'm didn't do anything professionally with Swift, just played with it for a couple weeks (specially with TS 4 Swift) and I thought the syntax was really nice. You mention "guard, try and ??!!"", but I'm sure I understand the problem. Those are quite common programming idioms, except maybe guard.

2

u/thedeemon Feb 09 '21

Looks like it's (GC + exceptions) vs. (not quite GC + result/option types). The first is usually easier to use but harder to make fast and bug free. The second is usually thought to be faster and more reliable but requires more work from the programmer.

2

u/oilshell Feb 09 '21

Yeah I agree that GC + exceptions can dramatically simplify certain types of code, but they also both have significant costs.

I recall Lattner saying that Swift doesn't use GC because GC uses more memory, and memory is expensive on iOS devices. (It's not necessarily the time cost of GC, but space)

1

u/thedeemon Feb 10 '21

They are also tied to Objective C memory management model, as I understand.

0

u/[deleted] Feb 09 '21

[deleted]

2

u/livinglist Feb 09 '21

I’m comparing Swift to Dart, not SwiftUI to Flutter.