<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Daniel Duan's Articles About POSIX</title>
        <link>https://duan.ca/tag/posix/</link>
        <atom:link href="https://duan.ca/tag/posix/feed.xml" rel="self" type="application/rss+xml" />
            <item>
                <title>Kick-ass CLI Tools In Swift</title>
                <description>&#60;p&#62;As someone who lives in a terminal simulator, I&#39;m pleasantly surprised by the
new toys we get in recent years such as &#60;a href=&#34;https://github.com/junegunn/fzf&#34;&#62;fzf&#60;/a&#62;, &#60;a href=&#34;https://github.com/BurntSushi/ripgrep&#34;&#62;ripgrep&#60;/a&#62;, &#60;a href=&#34;https://github.com/sharkdp/fd&#34;&#62;fd&#60;/a&#62;, etc.
A great number of these are written in relatively young programming languages
such as Go and Rust. But, noticibly, none of them are written in Swift.&#60;/p&#62;
&#60;p&#62;In this post, I&#39;ll try to explain why that is.&#60;/p&#62;
&#60;h2&#62;POSIX Ergonomics&#60;/h2&#62;
&#60;p&#62;Unix-like virtual file systems has been around for decades. API that manupulates
such systems has standardized a long time ago and exists in most computers
running Linux/BSD/macOS today (and, to a large extend, smart phones). To Swift
users, Using these APIs is straight-forward (&#60;code&#62;rmdir(&#38;quot;path/to/dir&#38;quot;)&#60;/code&#62;).&#60;/p&#62;
&#60;p&#62;So Swift programmers are all happy campers (re-)inventeing all sorts of file
system utilities, right?&#60;/p&#62;
&#60;p&#62;Well, not quite.&#60;/p&#62;
&#60;p&#62;Okay, I lied about POSIX APIs being &#38;quot;straight-forward&#38;quot; in Swift. Or rather, this
is very subjective.&#60;/p&#62;
&#60;p&#62;Continuing with the &#60;code&#62;rmdir&#60;/code&#62; example, we must first import it from either &#60;code&#62;Glibc&#60;/code&#62;
or &#60;code&#62;Darwin&#60;/code&#62;, depending on your OS. To know whether the operation is successful,
we need to see whether it returned integer 0. To learn &#60;em&#62;why&#60;/em&#62; 0 was not returned,
we need to read the &#38;quot;magical&#38;quot; variable &#60;code&#62;errno&#60;/code&#62;. &#60;code&#62;errno&#60;/code&#62; could be written to by
other APIs so we&#39;d better capture it in time…&#60;/p&#62;
&#60;p&#62;And that&#39;s one of the simpler APIs in POSIX calls!&#60;/p&#62;
&#60;p&#62;Programmers whine about ergonomics partially because we are previlidged and
spoiled. But mostly because our attention is a limited resources. Mixing API
conventions distracts us from solving the problem at hand. Bad ergonomics,
therefore, drives away a good potion of users who cares about quality of their
tools.&#60;/p&#62;
&#60;h2&#62;Culture and History&#60;/h2&#62;
&#60;p&#62;As of this writing, the release of Swift 5 is imminent. The vast majority of
existing Swift code is written to run on iOS. The concept of a file, or the
traditional virtal file system, is hidden to iOS users, and sandboxed for
developers. I bet most Swift users rarely think about the fact that there&#39;s
a entire set of POSIX API at their disposal.&#60;/p&#62;
&#60;p&#62;&#60;code&#62;Foundation&#60;/code&#62; alleviates the need to deal with files and directories: &#60;code&#62;Bundle&#60;/code&#62;
locates the files; &#60;code&#62;CoreData&#60;/code&#62;, &#60;code&#62;UserDefaults&#60;/code&#62; or the keychain is your primary
way to persist data; &#60;code&#62;Data&#60;/code&#62;, &#60;code&#62;String&#60;/code&#62; or &#60;code&#62;NSCoding&#60;/code&#62; has methods to read and
write to files.  And finally, if you really need to deal with files,
&#60;code&#62;NSFileManager&#60;/code&#62; has everything you&#39;ll ever need.&#60;/p&#62;
&#60;p&#62;Why would a productive Swift programmer think about POSIX in this environment?
Why would a tutor teach POSIX over the useful/practical/&#38;quot;native&#38;quot; alternatives?&#60;/p&#62;
&#60;p&#62;We can trace &#38;quot;riding on the Apple platform&#38;quot; mentality back to the pre-iPhone
days, where a very small Mac developer community labors on on a niche platform
(compared to iOS today) and they &#60;em&#62;loved&#60;/em&#62; it. However, I&#39;m sure they used more
POSIX stuff back then than the average iOS developers today.&#60;/p&#62;
&#60;p&#62;Having a great library such as Foundation on the most popular developer
platform where the language thrives means it&#39;ll take longer for &#38;quot;subcultures&#38;quot;
to emerge, if they do at all.&#60;/p&#62;
&#60;h2&#62;The Standard Library And Its Influence on New Users&#60;/h2&#62;
&#60;p&#62;File system APIs being in &#60;code&#62;Foundation&#60;/code&#62; as opposed to the standard library is
probably a temporary condition. Nevertheless, it has at least the following
implications:&#60;/p&#62;
&#60;ol&#62;
&#60;li&#62;
&#60;p&#62;Its quality of implementation is not held on the same standard that those
APIs in the standard library. This is especially true for the separate,
open-source &#60;code&#62;Foundation&#60;/code&#62; implementation. Getting consistent and correct
behaviors across macOS and Linux is hard.&#60;/p&#62;
&#60;/li&#62;
&#60;li&#62;
&#60;p&#62;A person learning Swift won&#39;t explore the language with a file system API.
This I suspect, is &#60;strong&#62;the most important reason many of these great CLI
utilites are written in other programming languages&#60;/strong&#62;. Programmers seek
instant gratification when they learn. And they usually stay in a limited
domain (like iOS) at first. This is where the built-in library is special: no
matter which domain is chosen, it&#39;s always available. Languages such as Go
and Rust include things like paths and files in their built-in library.
Playing with these APIs while learning the lanugage plants a seed for future,
serious, projects. There are less users of these languages compared to Swift,
but there are more people thinking about projects that involves file systems
in thoes communities. (Note I don&#39;t have statistics here, just a guess.)&#60;/p&#62;
&#60;/li&#62;
&#60;/ol&#62;
&#60;h2&#62;Conclusion&#60;/h2&#62;
&#60;p&#62;The next killer CLI tool is still more likely to be written in Go or Rust,
than in Swift. Hopefully, somewhere in these speculations is a true cause of
this phenomena. Maybe someone reading this will be inspired to accelerate change
that will eventually revert the condition. (I&#39;m &#60;a href=&#34;https://github.com/dduan/Pathos&#34;&#62;trying&#60;/a&#62;).&#60;/p&#62;
</description>
                <pubDate>Sun, 20 Jan 2019 16:09:38 -0800</pubDate>
                <link>https://duan.ca/2019/01/20/kick-ass-cli-tools-in-swift/</link>
                <guid isPermaLink="true">https://duan.ca/2019/01/20/kick-ass-cli-tools-in-swift/</guid>
            </item>
    </channel>
</rss>