Few questions regarding this language

Hi, I have a few questions regarding to Swift

  1. Is it supposed to be as fast as C/C++ and Rust?
  2. Is there a garbage collector or by default does it use reference counting?
  3. Is Swift just as safe as Rust?
  4. Does Swift offer low level features that is present in C/C++?
  5. Is this language easier to learn compared to C/C++ and Rust?
  6. Is this a system's programming language?

I don't have enough expertise in Rust, but I compare Swift and C++.

  1. In theory yes, but usage patterns are different. C++ gives a bit of bias towards manual memory management, while Swift suggests atomic reference counting as default memory management strategy.
  2. Reference counting, no tracing GC
  3. [Skip]
  4. Yes, using unsafe pointers and bitcasts you can implement anything that can be implemented in C/C++. Some tricks that can be done with C++ templates cannot be done with Swift generics, but code generation can be used as a workaround.
  5. Definitely easier that C/C++.
  6. It is mostly used for applied programming. I'm not aware of any example drivers or OS kernels in Swift, but in theory it should be possible. The biggest challenge I think would be porting the language runtime library.
1 Like

Is this enabled by default?

All class instances participate in Automatic Reference Counting (ARC) by default. You can "opt-out" of it using unsafe APIs, including manually manage reference count, manually allocate/deallocate data, etc.

Note also that struct doesn't have reference count. They are treated like a very big Int, passing around as a copy and all that. Some structs do contain class though, which is still subjected to ARC.

1 Like

[quote="unix1232, post:1, topic:41956"]

  • Is it supposed to be as fast as C/C++ and Rust?
    Answer: Yes, it is on an average unless you are going down to nanoseconds measurement

  • Is there a garbage collector or by default does it use reference counting?
    Answer: It uses reference counting - ARC - Explained - YouTube

  • Is Swift just as safe as Rust?
    Answer: Yes it is, swift prevents common gotcha's like buffer overflows and memory references, but then swift still supports you writing obj c or c code in it and if you squander away from the high level APIs then you can do some naughty things.

  • Does Swift offer low level features that is present in C/C++?
    Answer: Yes, as explained above. Method swizzling and other low level features are available. Now, if you end up using some prohibited low level feature and made an iOS App then Apple may still reject you.

  • Is this language easier to learn compared to C/C++ and Rust?
    Answer: The language simply flows so it is one of the easiest languages to wrap your arms around. You can start at the online video tutorial here: http://bit.ly/iOSLearning. You can skip the first lecture unless you want to understand the options available to compile and run swift outside playgrounds.

  • Is this a system's programming language?
    [/quote] No, it can be though. It is open sourced and porting of language runtime will be required. You have to think from the perspective that if there is an incumbent in the market then there is a challenge to over throw the incumbent. Java succeeded because Java provided something others didn't - write once run anymore - at the compromise of speed. Swift is succeeding in Apple world because Apple is pushing for it - it is modern, it is good but incentives for porting away from incumbent Obj C is provided by Apple. Now, if you don't know Obj C then i can't ask to go and learn it - it will be painful.