Hacker News new | ask | show | jobs
by manmal 3722 days ago
It's a systems language, and imo way more beautiful than c++.
3 comments

Absolutely. I'm not attempting to put it down as somehow not worthy of anyones time, just thought I'd mention how "cushioned" it all felt :P
What language do you prefer over Swift? I'm curious.
C++11 with review-enforced RAII. Boost if you need special stuff involving odd data structures. gtest/gmock. Depending on what you're doing, add some Qt5, OpenCV, OpenMP, RapidXML/RapidJSON, any C library ever written - and there are BSD/LGPL libraries for pretty much anything. Make plugins with Lua. Run tests in Valgrind.
I'm not sure I would choose something over the latest version, primarily because of how little a time investment using it can be... but then again it all comes down to what the task at hand is doesn't it ;P
Yeah but it can't replace C++. Swift was designed to make programming fun/nice not to replace C or C++.
"Swift is intended as a replacement for C-based languages (C, C++, and Objective-C). "

Taken from https://swift.org/about/

"Swift is a successor to both the C and Objective-C languages."

Taken from https://developer.apple.com/swift/

Swift can't do realtime. It has automatic memory management. How do you replace C or C++ with Swift for that? What they mean is that Swift replaces Objective-C on user facing apps(on Apple devices) but I doubt you will see the critical parts of the OS (kernel, drivers, etc) written in Swift.

It's almost like saying javascript will replace C/C++. I want a nice language like Swift (or Go) to replace C but there is none. Rust was the best/latest failed attempt.

You can essentially write C in Swift by using UnsafePointer and such. If you avoid using reference types then you won't get any automatic memory management. I don't think it's quite ready to replace C as a kernel or driver language, but it's not that far off.
Is there any open source library that does that? I know in theory many things are possible but it would be great to actually test it/see it in action. I never thought of Swift as a language with zero cost abstractions and deterministic performance. If it works I think it really gives it a leg up.
It's pretty uncommon, so I'm not sure. If I may toot my own horn a bit, you may find the code to my memory dumper interesting:

https://github.com/mikeash/memorydumper2

I also gave a talk on this subject a couple of years ago. It's out of date by now, but the basics are still more or less intact:

https://vimeo.com/107707576

Or peruse the standard library documentation for UnsafePointer, UnsafeRawPointer, UnsafeMutablePointer, and UnsafeRawMutablePointer. These are built-in types that the compiler knows about, and code that uses them should compile down to essentially the same stuff as C pointer code would.

This is especially true in Swift 3, which has many improvements around this type of programming.
> Swift can't do realtime. It has automatic memory management. How do you replace C or C++ with Swift for that?

Of course it can, it is just a matter of OS vendors caring to push it down developers throats.

There are ways to manually manage memory if required to do so, but then you suffer the consequences of dealing with Unsafe*.

Those are the claims. The reality does not match those claims.
Not, yet.

That is how systems programming languages get adopted, when OS vendors do "my way or the highway".

Agreed. Certainly if the dev has any regard for things like performance and security
It's not really predictable enough for a systems language.
Can you predict what your C compiler with going to do with your code, taking into account UB and compiler specific implementation behaviors not specified by ANSI C?
More than with Swift. I routinely see 100x - 1000x and more performance difference between -O0 and -O in Swift. Considering that the optimiser doesn't give warnings or errors if it can't apply optimisations, that's out of bounds for me for a systems programming language. YMMV.

The whole UB idiocy is a different matter, though related because it's perpetrated by roughly the same group of people, for similar nonsensical and non-validated reasons. See my post http://blog.metaobject.com/2014/04/cc-osmartass.html

See also: http://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_201... and Proebsting's Law.

> More than with Swift. I routinely see 100x - 1000x and more performance difference between -O0 and -O in Swift. Considering that the optimiser doesn't give warnings or errors if it can't apply optimisations, that's out of bounds for me for a systems programming language. YMMV.

Improvements required for the toolchain in a young language, than Apple, IBM and others will certainly improve.