The recent errno thread got me to this idea:
Would there be an interest of having a "swiftified" version of Darwin / Posix API's? Among the features we could have:
- errno -> throw conversion.
- no fear of ARC messing with errno.
- error handling standardized via throws (to not deal with the differences of particular API conventions (error condition in Darwin API's could be signalled by many different ways depending upon API: NULL, -1, < 0, <= 0).
- normal and convenient String / Data support for function parameters, including inout parameters, return values, string and data pointers stored in structures, etc. the resulting API is much safer and convenient IRT:
- memory allocations and ownership
- memory leaks prevention
- buffer overflow prevention
- array support when convenient, e.g. some API's could be restructured to return arrays instead of linked lists:
getifaddrs
could return an array, and there is no need to callfreeifaddrs
on that array. Int32
is substituted forInt
as a more conventient Swift type.- type safe constants (instead of
PF_INET
,SOCK_DGRAM
, etc) – would make API much less error prone. - better swifty naming for those constants (
.inet
,.datagram
, etc). - flags are passed as option sets.
- as in
fopen
example presented the mode parameter like "r", "w+", etc are also represented as type safe option sets.
- as in
- potentially we could prettify the naming (like "creat",
mkfifo
, etc)- this might include external labels for function parameters where deemed needed.
- We would have a proper documentation for those "wrapper" API's instead of relying on "man" pages.
Minimal usage example to illustrate some of the above bullet points:
var resolved = ""
let result: String = try realPath(path, &resolved)
let file: File = try fileOpen(path, [.read, .binary])
let s: Int = try socket(.inet, .stream)