2 ms·
> 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 imp
by 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.