Hacker News new | ask | show | jobs
by pjmlp 2340 days ago
Here,

https://swift.org/getting-started/#using-the-repl

> On macOS

    1> import Darwin
    2> arc4random_uniform(10)
    $R0: UInt32 = 4
> On Linux

    1> import Glibc
    2> random() % 10
    $R0: Int32 = 4
Any of the languages that Swift is competing against, doesn't need to have OS specific imports for basic stuff.
4 comments

Having distinct Darwin or Glibc imports seems a little crazy for these examples. When you code in c, libc is just there. It has minor variations from platform to platform, but you always think of it as libc. (Even if Darwin calls it libsystem etc. Or if Microsoft's is mscvrt and it kind of sucks.) It would be more reasonable to call it libc. (If I run on BSD do I still call it "glibc" with no actual GNU present? And if the arc4random APIs present in libc there look a lot like Darwin's because they actually share code? Maybe they have not considered porting to non-linux?)

Basically they set a portability boundary in the wrong place.

But for context, the previous sentence on that page is: “You can also import any available system modules, such as Darwin on macOS and Glibc on Linux”

The random number examples aren’t saying that’s the right way to generate random numbers in a platform-independent way, it is specifically demonstrating how to import system libraries on your local platform.

Swift 4.2 (2018) introduced a cross platform random number API.

    (0..<10).randomElement()
Yet they didn't bother to update the example on the introduction to Swift.

There are plenty of other examples I can look for, like file handling.

File handling can be done using Foundation, which I believe has API parity between Darwin and Linux at least.

I think the point of the example was to demonstrate importing platform-specific modules, not random number generation per se. But you're right, it should probably be updated to do something else.

>Yet they didn't bother to update the example on the introduction to Swift.

There are widely popular FOSS languages with much much less documentation...

Such as?
Golang has terser and worse documentation than Swift, for one...
Dude you’re literally illustrating Swifts strength here. The whole point is to have a safe easy to use language that reuses existing system libraries.
Any mainstream programming language can do that.
Not many use an actual c/c++ compiler (clang) to generate language native calling convention wrappers for calling into C/ObjC code (not sure how well c++ interop works these days).
Few as elegant, as safe, or as performant combined...
Delphi, D, .NET, Eiffel, Ada, Rust, Nim.

And lets not put Swift and performance on the same sentence, they still need to catch up a bit.

https://github.com/ixy-languages/ixy-languages

What is this, a cabinet of curiosities?

Aside from .NET and Rust, all the others are not players in 2020.

Ah, that might be the reason why NVidia is adopting Ada for their firmware, ebay and Netflix decided to use D for some of their projects, the Turkish government decided to invest into Delphi for their education system,...

By the way, it is a list of languages with compilers that currently outperform Swift.