Some More Adventures in Swiftland

Last summer, I tried to learn new tricks and got some small success. Because the pace at which Swift 3 is changing finally settled a bit, I decided to finish what I set out to do and make SwiftyDB both up to date with the latest version and working on Linux.

The beginning

In August, SwiftyDB was working fine on MacOS but, while it compiled fine, it didn’t on Linux for a variety of reasons.

Swift was flimsy on that platform. The thing “worked”, but caused weird errors, had strange dependancies and was severely lacking stuff in the Foundation library. The version I had then crashed all the time, but for random and different reasons, so I decided to wait till it stabilized.

With Swift 3.0.2 and the announcement that Kitura was to become one of the pillars of the official built-in server apis (called it, by the way), I figured it was time to end the migration.

The problem

The main issue is that swift for Linux lacks basic things Foundation on the Mac has. I mean, it doesn’t even have NSDate’s timeIntervalSinceReferenceDate… But beyond that, the port lacks something that is truly important for the kind of framework that SwiftyDB is : introspection.

The typechecker is the absolute core of Swift. Don’t get me wrong, it’s great. It forces people to mind the type of the data they are manipulating, and throws errors early rather than late. But it comes at a cost : the compiler does all kinds of crazy operations to try to guess the type and too often for my tastes, fails miserably. If you’ve ever seen “IDE internal error” dialogs in XCode, that’s probably the reason.

But even if it worked perfectly, data manipulation that’s needed to get rows in and out of the db requires either to work with formless data (JSON) or to have a way to coerce and map types at run time. And boy, swift doesn’t like that at all.

So, SwiftyDB handles it in a hybrid way, passing along dictionaries of type [String : Any] and suchlikes. It’s kind of gross, to be honest, but that’s the only way this is going to work.

But that was kind of solved in August, albeit in a crash-prone way. What was the issue this time?

The swift team made huge strides to unify MacOS and Linux from an API point of view. If you read the doc, it “just works”, more or less. And that’s true, except for one tiny little thing : toll-free bridging.

Data type conversion

Swift, as ObjectiveC before it, deals with legacy stuff by having a toll-free bridging mechanism. Basically, to the compiler, NSString and String are interchangeable, and will use the definition (and the methods) it needs based on the line it’s at, rather than as a static type.

Something you surely know if you’ve done any kind of object oriented programming, typecasting is hard. If String inherits from NSString, I can use an object of the former type in any place I would have to use the latter. Think of the relationship between a square and a rectangle. The square is a rectangle, but the rectangle isn’t necessarily a square. It’s an asymmetrical relationship. And you can’t make it work by also having NSString inheriting from String, because that’s not allowed for a lot of complicated reasons, but with effects you can probably guess.

So, how does this toll-free bridging work? By cheating. But that’s neither here nor there. The fact is that it works just fine on MacOS, and not on Linux.

A solution

The easiest way to solve that is to have constructors in both classes that take the other as a parameter. And that’s the way it’s solved on Linux. True it’s a bit inelegant, and negates most of the “pure sexiness” that swift is supposed to have, but what are you gonna do? This, after all, is still a science.

Once those extensions are in place, as well as a few replacement additions to the stock Foundation (such as the infamous timeIntervalSinceReferenceDate), and a few tweaks to the way the system finds the SQLite headers, everything finally works.

Use it

As I said before it’s mostly an intellectual exercise, and a way to see if I could some day write some serverside stuff, but in the meantime it works just fine and you can find it here. Feel free to submit pull requests and stuff, but as it is, it works as intended : swift objects to sqlite storage and vice versa.

As usual, if you want to use it as a swift package, just use:

.Package(url: "https://github.com/krugazor/swiftydb", majorVersion: 1, minor: 2)

  

To be, or not to be (a geek)?

According to this site, I am a Super Geek. While the word doesn’t hold such a big meaning around here, it got me wondering what it was supposed to be about.

The “geek status” is something that’s typical of high school (and even more so in the US, where geeks and nerds are commonly ostracized… being “popular” is all that matters). Originally it meant “someone who doesn’t fit”. It was someone who didn’t go to parties, wasn’t good with girls, or at sports, and used his brain more often than his legs.

With the advent of the personal computers, a whole new species came into being : the Geek (notice the capital). He is a geek, but mainly because computers are all his life. He doesn’t really want to fit in, he has his world elsewhere, in the virtual universe.

In France, the “geek status” doesn’t really exist. Of course there are bullies, popularity with girls and everything, but the closest equivalent we can find is the “fayot”. The student that lives to please the teacher. During lunch breaks, most of the boys would play soccer (even when they sucked, it’s all part of the game), or cards. And most of the girls would talk among themselves. Of course, just like the geek, it’s a crass approximation, but that’s how it was in my high school.

But let’s get back to this whole “I am a geek” thing. As far as I understand it, being a geek is equivalent to being “a guy who sees the sun maybe twice a year, has all his friends online, and collects trivia about minor matters”.

Funnily enough, that’s what I think about the younger generation. Most of the teens I know are exactly like this. They prowl through MSN/mySpace/… and feel a lot more comfortable there than with the human counterparts that cross their paths IRL (in the real life, or something…).

So, am I a geek? I don’t feel like I fit the profile (except for the “not good with girls” :> ). But it’s true that I collect trivia about things that may or may not matter. I just devour books about anything… even books I don’t like. I like science fiction, and heroic fantasy books (among other things), because it’s a way for authors not to worry about “reality” and focus on the human traits. If you don’t have to worry about death, space, or money, how’s life?

I don’t resent being called a geek, I just feel like it’s a word that doesn’t mean anything anymore. Just like “computer guy”, really. It just encompasses half of the computer users on the planet. Including secretaries and tax collectors.

That said, I’ll keep the “Super” from “SuperGeek” proudly :P