4 ms·
> That’s why Swift’s expressive and very lightweight syntax And a few paragraphs later: extension Array { func sorted<T: Comparable>( by keyPath
by dmitriid 7y ago
> That’s why Swift’s expressive and very lightweight syntax
And a few paragraphs later:
extension Array {
func sorted<T: Comparable>(
by keyPath: KeyPath<Element, T>
) -> Self {
sorted { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
}
}
I can't say this is lightweight any longer.
My greatest gripe with Swift comes not from Swift itself but from its standard library. When most new languages start with their own standard libraries that are usually interfaces towards C APIs or POSIX, Swift's Foundation is half of MacOS and then some.
I recently needed to read some data from a file in Swift, and it took me quite some time to realise there was no fopen equivalent. Read/write files is a function on String? And getting a file handle is an excercise in figuring out proper dir names and user enums? [1]
There are a few other weird things in the language itself like guards which IMO are an entirely unnecessary construct that can be as easily solved with regular ifs. Or Optionals that have no way of working with them except unwrapping them (either with ! or via guards).
SwiftUI, however, is a very interesting approach, and I hope there are more frameworks (especially UI frameworks) like this.
[1] https://twitter.com/dmitriid/status/1201441652507844608 https://twitter.com/dmitriid/status/1201441652507844608
- saagarjha 7y ago> I can't say this is lightweight any longer. Swift optimizes for lightness at the call site, but even the definition is quite short for the constraints it imposes. > I recently needed to read some data from a file in Swift, and it took me quite some time to realise there was no fopen equivalent. You can import Darwin or Glibc to get direct access to fopen. It’s a bit unergonomic to use, though. > Read/write files is a function on String? NSString, as a deprecated holdover from Foundation. > There are a few other weird things in the language itself like guards which IMO are an entirely unnecessary construct that can be as easily solved with regular ifs. That’s the whole point of syntactic sugar and constructs which make your intent clear. > Or Optionals that have no way of working with them except unwrapping them (either with ! or via guards). There’s also the nil-coalescing operator, but the whole point is that you can’t sweep an Optional under the rug.
- dmitriid 7y ago> but even the definition is quite short for the constraints it imposes. I'd have to look up how extension method would look in this case in C#, but my hunch is that it would be more lightweight and readable, with the same contstraints. > You can import Darwin or Glibc to get direct access to fopen. It’s a bit unergonomic to use, though. FileManager.default, .appendingPath etc. isn't ergonomic either ;) > NSString, as a deprecated holdover from Foundation. 1. How is it deprecated if that's clearly the way to read/write files? 2. Foundation is the standard lib for Swift, and the way it's designed hurts Swift-the-language immensely, IMO. If that's deprecated, what's not deprecated and how can anyone figure that out? > There’s also the nil-coalescing operator, but the whole point is that you can’t sweep an Optional under the rug. There are several languages with Optionals, and they don't let you sweep them under the rug, and they provide better facilities to handle them. IMO optionals the way they are (and a few other decisions) come precisely from the constraints of Foundation and original goals of Swift: to be a replacement for Obj-C when creating iPhone apps. That's why the NSStrings, and the awkward APIs, and the astonishingly large number of nullable parameters in API calls and nullable results from API calls.
- cableshaft 7y agoThe directory thing is more due to sandboxing than anything else. They don't want someone to be able to write an app that can read anything anywhere on your phone, so it has specific directories that have different levels of access and temporality (i.e. one is a temp folder that gets flushed each time you quit the app, if I remember correctly). It's not that complicated once you look it up, and like most of this stuff, you can mostly write wrapper classes one time to hide the gnarly bits and call your wrapper class to do what you need to do the way you like to do it. It's been several years since I've had it read from a file, and I actually thought I would click that twitter link and see something super complicated, but it's actually pretty straight-forward. Not a one-liner like some languages, no, but as soon as you want to open something with options (like the encoding in this one), most other languages start looking more complicated like this as well. Like python's open looks more like this with options: "open(filename, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)". I've opened many a file in Python scripts, but I'd have to look up buffering, encoding, errors, closefd, and opener right now if I wanted to use them (I haven't really in the past) And Python has 11 different access modes that I often have to look up to remember what all my options are again: r+,w,wb,w+,wb+,a,ab,a+, and ab+
- dmitriid 7y ago> They don't want someone to be able to write an app that can read anything anywhere on your phone, And that's the main problem with Foundation which hurts Swift as a language: it was made for iPhone apps, and hardly anything else. I was trying to make a CLI app on the desktop. I gave up and used something else. > It's not that complicated once you look it up, and like most of this stuff Yeah, I tried to look it up. It's extremely complicated, and it doesn't help that the documentation tends to spread all the knowledge across dozens of pages with very terse documentation and next to zero examples. > I've opened many a file in Python scripts, but I'd have to look up buffering, encoding, errors, closefd, and opener right now if I wanted to use them My example doesn't have any buffering, or encoding, or anything. It's just reading a file. There's no chance in hell you can deduce those lines from the documnentation, or find out where to put the files to read from, or find out that it's not the file's job to read/write data, but String type's (wat?) etc. It doesn't help that the only real example of reading and writing files isn't in the docs, but in a StackOverflow answer (most other examples are either outdated, or use calls to Obj-C libs etc.)
- fauigerzigerk 7y ago>I can't say this is lightweight any longer. Compared to what? What this code does can be rather convoluted and/or unsafe in many statically typed languages.